Exemplo n.º 1
0
    def _GetObjectRef(self, bucket_url_string, gcs_object, with_version=False):
        """Creates a BucketListingRef of type OBJECT from the arguments.

    Args:
      bucket_url_string: Wildcardless string describing the containing bucket.
      gcs_object: gsutil_api root Object for populating the BucketListingRef.
      with_version: If true, return a reference with a versioned string.

    Returns:
      BucketListingRef of type OBJECT.
    """
        # Generation can be None in test mocks, so just return the
        # live object for simplicity.
        if with_version and gcs_object.generation is not None:
            generation_str = GenerationFromUrlAndString(
                self.wildcard_url, gcs_object.generation)
            object_string = '%s%s#%s' % (bucket_url_string, gcs_object.name,
                                         generation_str)
        else:
            object_string = '%s%s' % (bucket_url_string, gcs_object.name)
        object_url = StorageUrlFromString(object_string)
        return BucketListingObject(object_url, root_object=gcs_object)
Exemplo n.º 2
0
def PrintFullInfoAboutObject(bucket_listing_ref, incl_acl=True):
    """Print full info for given object (like what displays for gsutil ls -L).

  Args:
    bucket_listing_ref: BucketListingRef being listed.
                        Must have ref_type OBJECT and a populated root_object
                        with the desired fields.
    incl_acl: True if ACL info should be output.

  Returns:
    Tuple (number of objects, object_length)

  Raises:
    Exception: if calling bug encountered.
  """
    url_str = bucket_listing_ref.url_string
    storage_url = StorageUrlFromString(url_str)
    obj = bucket_listing_ref.root_object

    if (obj.metadata
            and S3_DELETE_MARKER_GUID in obj.metadata.additionalProperties):
        num_bytes = 0
        num_objs = 0
        url_str += '<DeleteMarker>'
    else:
        num_bytes = obj.size
        num_objs = 1

    text_util.print_to_fd('{}:'.format(url_str))
    if obj.timeCreated:
        text_util.print_to_fd(
            MakeMetadataLine(
                'Creation time',
                obj.timeCreated.strftime('%a, %d %b %Y %H:%M:%S GMT')))
    if obj.updated:
        text_util.print_to_fd(
            MakeMetadataLine(
                'Update time',
                obj.updated.strftime('%a, %d %b %Y %H:%M:%S GMT')))
    if (obj.timeStorageClassUpdated
            and obj.timeStorageClassUpdated != obj.timeCreated):
        text_util.print_to_fd(
            MakeMetadataLine(
                'Storage class update time',
                obj.timeStorageClassUpdated.strftime(
                    '%a, %d %b %Y %H:%M:%S GMT')))
    if obj.storageClass:
        text_util.print_to_fd(
            MakeMetadataLine('Storage class', obj.storageClass))
    if obj.temporaryHold:
        text_util.print_to_fd(MakeMetadataLine('Temporary Hold', 'Enabled'))
    if obj.eventBasedHold:
        text_util.print_to_fd(MakeMetadataLine('Event-Based Hold', 'Enabled'))
    if obj.retentionExpirationTime:
        text_util.print_to_fd(
            MakeMetadataLine(
                'Retention Expiration',
                obj.retentionExpirationTime.strftime(
                    '%a, %d %b %Y %H:%M:%S GMT')))
    if obj.kmsKeyName:
        text_util.print_to_fd(MakeMetadataLine('KMS key', obj.kmsKeyName))
    if obj.cacheControl:
        text_util.print_to_fd(
            MakeMetadataLine('Cache-Control', obj.cacheControl))
    if obj.contentDisposition:
        text_util.print_to_fd(
            MakeMetadataLine('Content-Disposition', obj.contentDisposition))
    if obj.contentEncoding:
        text_util.print_to_fd(
            MakeMetadataLine('Content-Encoding', obj.contentEncoding))
    if obj.contentLanguage:
        text_util.print_to_fd(
            MakeMetadataLine('Content-Language', obj.contentLanguage))
    text_util.print_to_fd(MakeMetadataLine('Content-Length', obj.size))
    text_util.print_to_fd(MakeMetadataLine('Content-Type', obj.contentType))
    if obj.componentCount:
        text_util.print_to_fd(
            MakeMetadataLine('Component-Count', obj.componentCount))
    if obj.timeDeleted:
        text_util.print_to_fd(
            MakeMetadataLine(
                'Archived time',
                obj.timeDeleted.strftime('%a, %d %b %Y %H:%M:%S GMT')))
    marker_props = {}
    if obj.metadata and obj.metadata.additionalProperties:
        non_marker_props = []
        for add_prop in obj.metadata.additionalProperties:
            if add_prop.key not in S3_MARKER_GUIDS:
                non_marker_props.append(add_prop)
            else:
                marker_props[add_prop.key] = add_prop.value
        if non_marker_props:
            text_util.print_to_fd(MakeMetadataLine('Metadata', ''))
            for ap in non_marker_props:
                ap_key = '{}'.format(ap.key)
                ap_value = '{}'.format(ap.value)
                meta_data_line = MakeMetadataLine(ap_key, ap_value, indent=2)
                text_util.print_to_fd(meta_data_line)
    if obj.customerEncryption:
        if not obj.crc32c:
            text_util.print_to_fd(
                MakeMetadataLine('Hash (crc32c)', 'encrypted'))
        if not obj.md5Hash:
            text_util.print_to_fd(MakeMetadataLine('Hash (md5)', 'encrypted'))
        text_util.print_to_fd(
            MakeMetadataLine('Encryption algorithm',
                             obj.customerEncryption.encryptionAlgorithm))
        text_util.print_to_fd(
            MakeMetadataLine('Encryption key SHA256',
                             obj.customerEncryption.keySha256))
    if obj.crc32c:
        text_util.print_to_fd(MakeMetadataLine('Hash (crc32c)', obj.crc32c))
    if obj.md5Hash:
        text_util.print_to_fd(MakeMetadataLine('Hash (md5)', obj.md5Hash))
    text_util.print_to_fd(MakeMetadataLine('ETag', obj.etag.strip('"\'')))
    if obj.generation:
        generation_str = GenerationFromUrlAndString(storage_url,
                                                    obj.generation)
        text_util.print_to_fd(MakeMetadataLine('Generation', generation_str))
    if obj.metageneration:
        text_util.print_to_fd(
            MakeMetadataLine('Metageneration', obj.metageneration))
    if incl_acl:
        # JSON API won't return acls as part of the response unless we have
        # full control scope
        if obj.acl:
            text_util.print_to_fd(
                MakeMetadataLine('ACL',
                                 AclTranslation.JsonFromMessage(obj.acl)))
        elif S3_ACL_MARKER_GUID in marker_props:
            text_util.print_to_fd(
                MakeMetadataLine('ACL', marker_props[S3_ACL_MARKER_GUID]))
        else:
            # Empty ACLs are possible with Bucket Policy Only and no longer imply
            # ACCESS DENIED anymore.
            text_util.print_to_fd(MakeMetadataLine('ACL', '[]'))

    return (num_objs, num_bytes)
Exemplo n.º 3
0
 def _SingleVersionMatches(self, listed_generation):
     decoded_generation = GenerationFromUrlAndString(
         self.wildcard_url, listed_generation)
     return str(self.wildcard_url.generation) == str(decoded_generation)
Exemplo n.º 4
0
def PrintFullInfoAboutObject(bucket_listing_ref, incl_acl=True):
    """Print full info for given object (like what displays for gsutil ls -L).

  Args:
    bucket_listing_ref: BucketListingRef being listed.
                        Must have ref_type OBJECT and a populated root_object
                        with the desired fields.
    incl_acl: True if ACL info should be output.

  Returns:
    Tuple (number of objects, object_length)

  Raises:
    Exception: if calling bug encountered.
  """
    url_str = bucket_listing_ref.url_string
    storage_url = StorageUrlFromString(url_str)
    obj = bucket_listing_ref.root_object

    if (obj.metadata
            and S3_DELETE_MARKER_GUID in obj.metadata.additionalProperties):
        num_bytes = 0
        num_objs = 0
        url_str += '<DeleteMarker>'
    else:
        num_bytes = obj.size
        num_objs = 1

    print('%s:' % url_str.encode(UTF8))
    if obj.timeCreated:
        print(
            MakeMetadataLine(
                'Creation time',
                obj.timeCreated.strftime('%a, %d %b %Y %H:%M:%S GMT')))
    if obj.updated:
        print(
            MakeMetadataLine(
                'Update time',
                obj.updated.strftime('%a, %d %b %Y %H:%M:%S GMT')))
    if (obj.timeStorageClassUpdated
            and obj.timeStorageClassUpdated != obj.timeCreated):
        print(
            MakeMetadataLine(
                'Storage class update time',
                obj.timeStorageClassUpdated.strftime(
                    '%a, %d %b %Y %H:%M:%S GMT')))
    if obj.storageClass:
        print(MakeMetadataLine('Storage class', obj.storageClass))
    if obj.kmsKeyName:
        print(MakeMetadataLine('KMS key', obj.kmsKeyName))
    if obj.cacheControl:
        print(MakeMetadataLine('Cache-Control', obj.cacheControl))
    if obj.contentDisposition:
        print(MakeMetadataLine('Content-Disposition', obj.contentDisposition))
    if obj.contentEncoding:
        print(MakeMetadataLine('Content-Encoding', obj.contentEncoding))
    if obj.contentLanguage:
        print(MakeMetadataLine('Content-Language', obj.contentLanguage))
    print(MakeMetadataLine('Content-Length', obj.size))
    print(MakeMetadataLine('Content-Type', obj.contentType))
    if obj.componentCount:
        print(MakeMetadataLine('Component-Count', obj.componentCount))
    if obj.timeDeleted:
        print(
            MakeMetadataLine(
                'Archived time',
                obj.timeDeleted.strftime('%a, %d %b %Y %H:%M:%S GMT')))
    marker_props = {}
    if obj.metadata and obj.metadata.additionalProperties:
        non_marker_props = []
        for add_prop in obj.metadata.additionalProperties:
            if add_prop.key not in S3_MARKER_GUIDS:
                non_marker_props.append(add_prop)
            else:
                marker_props[add_prop.key] = add_prop.value
        if non_marker_props:
            print(MakeMetadataLine('Metadata', ''))
            for ap in non_marker_props:
                print(
                    MakeMetadataLine(('%s' % ap.key).encode(UTF8),
                                     ('%s' % ap.value).encode(UTF8),
                                     indent=2))
    if obj.customerEncryption:
        if not obj.crc32c:
            print(MakeMetadataLine('Hash (crc32c)', 'encrypted'))
        if not obj.md5Hash:
            print(MakeMetadataLine('Hash (md5)', 'encrypted'))
        print(
            MakeMetadataLine('Encryption algorithm',
                             obj.customerEncryption.encryptionAlgorithm))
        print(
            MakeMetadataLine('Encryption key SHA256',
                             obj.customerEncryption.keySha256))
    if obj.crc32c:
        print(MakeMetadataLine('Hash (crc32c)', obj.crc32c))
    if obj.md5Hash:
        print(MakeMetadataLine('Hash (md5)', obj.md5Hash))
    print(MakeMetadataLine('ETag', obj.etag.strip('"\'')))
    if obj.generation:
        generation_str = GenerationFromUrlAndString(storage_url,
                                                    obj.generation)
        print(MakeMetadataLine('Generation', generation_str))
    if obj.metageneration:
        print(MakeMetadataLine('Metageneration', obj.metageneration))
    if incl_acl:
        # JSON API won't return acls as part of the response unless we have
        # full control scope
        if obj.acl:
            print(
                MakeMetadataLine('ACL',
                                 AclTranslation.JsonFromMessage(obj.acl)))
        elif S3_ACL_MARKER_GUID in marker_props:
            print(MakeMetadataLine('ACL', marker_props[S3_ACL_MARKER_GUID]))
        else:
            print(MakeMetadataLine('ACL', 'ACCESS DENIED'))
            print(
                MakeMetadataLine(
                    'Note',
                    'You need OWNER permission on the object to read its ACL',
                    2))
    return (num_objs, num_bytes)