Opens a Google Cloud Storage file and returns it as a File-like object. Args: filename: A Google Cloud Storage filename of form '/bucket/filename'. mode: 'r' for reading mode. 'w' for writing mode. In reading mode, the file must exist. In writing mode, a file will be created o
(filename,
mode='r',
content_type=None,
options=None,
read_buffer_size=storage_api.ReadBuffer.DEFAULT_BUFFER_SIZE,
retry_params=None,
_account_id=None,
offset=0)
| 43 | |
| 44 | |
| 45 | def open(filename, |
| 46 | mode='r', |
| 47 | content_type=None, |
| 48 | options=None, |
| 49 | read_buffer_size=storage_api.ReadBuffer.DEFAULT_BUFFER_SIZE, |
| 50 | retry_params=None, |
| 51 | _account_id=None, |
| 52 | offset=0): |
| 53 | """Opens a Google Cloud Storage file and returns it as a File-like object. |
| 54 | |
| 55 | Args: |
| 56 | filename: A Google Cloud Storage filename of form '/bucket/filename'. |
| 57 | mode: 'r' for reading mode. 'w' for writing mode. |
| 58 | In reading mode, the file must exist. In writing mode, a file will |
| 59 | be created or be overrode. |
| 60 | content_type: The MIME type of the file. str. Only valid in writing mode. |
| 61 | options: A str->basestring dict to specify additional headers to pass to |
| 62 | GCS e.g. {'x-goog-acl': 'private', 'x-goog-meta-foo': 'foo'}. |
| 63 | Supported options are x-goog-acl, x-goog-meta-, cache-control, |
| 64 | content-disposition, and content-encoding. |
| 65 | Only valid in writing mode. |
| 66 | See https://developers.google.com/storage/docs/reference-headers |
| 67 | for details. |
| 68 | read_buffer_size: The buffer size for read. Read keeps a buffer |
| 69 | and prefetches another one. To minimize blocking for large files, |
| 70 | always read by buffer size. To minimize number of RPC requests for |
| 71 | small files, set a large buffer size. Max is 30MB. |
| 72 | retry_params: An instance of api_utils.RetryParams for subsequent calls |
| 73 | to GCS from this file handle. If None, the default one is used. |
| 74 | _account_id: Internal-use only. |
| 75 | offset: Number of bytes to skip at the start of the file. If None, 0 is |
| 76 | used. |
| 77 | |
| 78 | Returns: |
| 79 | A reading or writing buffer that supports File-like interface. Buffer |
| 80 | must be closed after operations are done. |
| 81 | |
| 82 | Raises: |
| 83 | errors.AuthorizationError: if authorization failed. |
| 84 | errors.NotFoundError: if an object that's expected to exist doesn't. |
| 85 | ValueError: invalid open mode or if content_type or options are specified |
| 86 | in reading mode. |
| 87 | """ |
| 88 | common.validate_file_path(filename) |
| 89 | api = storage_api._get_storage_api(retry_params=retry_params, |
| 90 | account_id=_account_id) |
| 91 | filename = api_utils._quote_filename(filename) |
| 92 | |
| 93 | if mode == 'w': |
| 94 | common.validate_options(options) |
| 95 | return storage_api.StreamingBuffer(api, filename, content_type, options) |
| 96 | elif mode == 'r': |
| 97 | if content_type or options: |
| 98 | raise ValueError('Options and content_type can only be specified ' |
| 99 | 'for writing mode.') |
| 100 | return storage_api.ReadBuffer(api, |
| 101 | filename, |
| 102 | buffer_size=read_buffer_size, |
no outgoing calls