def __setstate__(self, state): """Restore state as part of deserialization/unpickling. Args: state: the dictionary from a __getstate__ call """ self._api = state['api'] self._path_with_token = state['path_token'] self._buffer = state['buffer'] self._buffered = state['buffered'] self._written = state['written'] self._offset = state['offset'] self.closed = state['closed'] self._path = state['path'] self.name = api_utils._unquote_filename(self._path)
def __init__(self, api, path, buffer_size=DEFAULT_BUFFER_SIZE, max_request_size=MAX_REQUEST_SIZE): """Constructor. Args: api: A StorageApi instance. path: Quoted/escaped path to the object, e.g. /mybucket/myfile buffer_size: buffer size. The ReadBuffer keeps one buffer. But there may be a pending future that contains a second buffer. This size must be less than max_request_size. max_request_size: Max bytes to request in one urlfetch. """ self._api = api self._path = path self.name = api_utils._unquote_filename(path) self.closed = False assert buffer_size <= max_request_size self._buffer_size = buffer_size self._max_request_size = max_request_size self._offset = 0 self._buffer = _Buffer() self._etag = None get_future = self._get_segment(0, self._buffer_size, check_response=False) status, headers, content = self._api.head_object(path) errors.check_status(status, [200], path, resp_headers=headers, body=content) self._file_size = long(common.get_stored_content_length(headers)) self._check_etag(headers.get('etag')) self._buffer_future = None if self._file_size != 0: content, check_response_closure = get_future.get_result() check_response_closure() self._buffer.reset(content) self._request_next_buffer()
def __init__(self, api, path, content_type=None, gcs_headers=None): """Constructor. Args: api: A StorageApi instance. path: Quoted/escaped path to the object, e.g. /mybucket/myfile content_type: Optional content-type; Default value is delegate to Google Cloud Storage. gcs_headers: additional gs headers as a str->str dict, e.g {'x-goog-acl': 'private', 'x-goog-meta-foo': 'foo'}. Raises: IOError: When this location can not be found. """ assert self._maxrequestsize > self._blocksize assert self._maxrequestsize % self._blocksize == 0 assert self._maxrequestsize >= self._flushsize self._api = api self._path = path self.name = api_utils._unquote_filename(path) self.closed = False self._buffer = collections.deque() self._buffered = 0 self._written = 0 self._offset = 0 headers = {'x-goog-resumable': 'start'} if content_type: headers['content-type'] = content_type if gcs_headers: headers.update(gcs_headers) status, resp_headers, content = self._api.post_object(path, headers=headers) errors.check_status(status, [201], path, headers, resp_headers, body=content) loc = resp_headers.get('location') if not loc: raise IOError('No location header found in 201 response') parsed = urlparse.urlparse(loc) self._path_with_token = '%s?%s' % (self._path, parsed.query)
def __setstate__(self, state): """Restore state as part of deserialization/unpickling. Args: state: the dictionary from a __getstate__ call Along with restoring the state, pre-fetch the next read buffer. """ self._api = state['api'] self._path = state['path'] self.name = api_utils._unquote_filename(self._path) self._buffer_size = state['buffer_size'] self._max_request_size = state['request_size'] self._etag = state['etag'] self._file_size = state['size'] self._offset = state['offset'] self._buffer = _Buffer() self.closed = state['closed'] self._buffer_future = None if self._remaining() and not self.closed: self._request_next_buffer()
def EscapeUnescapeFilename(unescaped, escaped): self.assertEqual(escaped, api_utils._quote_filename(unescaped)) self.assertEqual(unescaped, api_utils._unquote_filename(escaped))