Beispiel #1
0
    def __init__(self, client, path, buffer_size=DEFAULT_READ_BUFFER_SIZE):
        self.client = client
        self.path = path
        self.bucket, self.name = parse_gcs_path(path)
        self.buffer_size = buffer_size

        # Get object state.
        get_request = (storage.StorageObjectsGetRequest(bucket=self.bucket,
                                                        object=self.name))
        try:
            metadata = self._get_object_metadata(get_request)
        except HttpError as http_error:
            if http_error.status_code == 404:
                raise IOError(errno.ENOENT, 'Not found: %s' % self.path)
            else:
                logging.error('HTTP error while requesting file %s: %s',
                              self.path, http_error)
                raise
        self.size = metadata.size

        # Ensure read is from file of the correct generation.
        get_request.generation = metadata.generation

        # Initialize read buffer state.
        self.download_stream = StringIO.StringIO()
        self.downloader = transfer.Download(self.download_stream,
                                            auto_transfer=False)
        self.client.objects.Get(get_request, download=self.downloader)
        self.position = 0
        self.buffer = ''
        self.buffer_start_position = 0
        self.closed = False
Beispiel #2
0
    def exists(self, path):
        """Returns whether the given gcs object exists.

    Args:
      path: GCS file path pattern in the form gs://<bucket>/<name>.
    """
        bucket, object_path = parse_gcs_path(path)
        try:
            request = storage.StorageObjectsGetRequest(bucket=bucket,
                                                       object=object_path)
            self.client.objects.Get(request)  # metadata
            return True
        except IOError:
            return False