Пример #1
0
  def get_object_metadata(self,
                          bucket_name,
                          object_name,
                          generation=None,
                          fields_scope=cloud_api.FieldsScope.NO_ACL):
    """See super class."""

    # S3 requires a string, but GCS uses an int for generation.
    if generation:
      generation = int(generation)

    projection = self._GetProjection(
        fields_scope, self.messages.StorageObjectsGetRequest)

    # TODO(b/160238394) Decrypt metadata fields if necessary.
    try:
      object_metadata = self.client.objects.Get(
          self.messages.StorageObjectsGetRequest(
              bucket=bucket_name,
              object=object_name,
              generation=generation,
              projection=projection))
    except apitools_exceptions.HttpNotFoundError:
      raise cloud_errors.NotFoundError(
          'Object not found: {}'.format(storage_url.CloudUrl(
              storage_url.ProviderPrefix.GCS, bucket_name, object_name,
              generation).url_string)
      )
    return _object_resource_from_metadata(object_metadata)
Пример #2
0
    def get_object_metadata(self,
                            bucket_name,
                            object_name,
                            generation=None,
                            fields_scope=None):
        """See super class."""
        request = {'Bucket': bucket_name, 'Key': object_name}

        # The VersionId keyword argument to head_object is not nullable if it is
        # present, so only include it in the function call if it has a value.
        if generation is not None:
            request['VersionId'] = generation

        try:
            object_dict = self.client.head_object(**request)
        except botocore.exceptions.ClientError as e:
            if e.response.get('ResponseMetadata',
                              {}).get('HTTPStatusCode') == 404:
                # Allows custom error handling.
                raise errors.NotFoundError('Object not found: {}'.format(
                    storage_url.CloudUrl(storage_url.ProviderPrefix.S3,
                                         bucket_name, object_name,
                                         generation).url_string))
            raise e

        # User requested ACL's with FieldsScope.FULL.
        if fields_scope is cloud_api.FieldsScope.FULL:
            try:
                acl_response = self.client.get_object_acl(**request)
                acl_response.pop('ResponseMetadata', None)
                object_dict['ACL'] = acl_response
            except botocore.exceptions.ClientError as error:
                object_dict['ACL'] = errors.S3ApiError(error)

        return _get_object_resource_from_s3_response(object_dict, bucket_name,
                                                     object_name)
def _raise_if_not_found_error(error, resource_name):
    if error.response.get('ResponseMetadata', {}).get('HTTPStatusCode') == 404:
        # TODO(b/193464904): Remove the hardcoded error message here after
        # refactoring the errors module.
        raise errors.NotFoundError('{} not found: 404.'.format(resource_name))
Пример #4
0
def _raise_if_not_found_error(error, resource_name):
    if error.response.get('ResponseMetadata', {}).get('HTTPStatusCode') == 404:
        raise errors.NotFoundError('Not found: {}'.format(resource_name))