def _PrintBucketInfo(self, bucket_blr, listing_style): """Print listing info for given bucket. Args: bucket_blr: BucketListingReference for the bucket being listed listing_style: ListingStyle enum describing type of output desired. Returns: Tuple (total objects, total bytes) in the bucket. """ if (listing_style == ListingStyle.SHORT or listing_style == ListingStyle.LONG): print bucket_blr return # listing_style == ListingStyle.LONG_LONG: # We're guaranteed by the caller that the root object is populated. bucket = bucket_blr.root_object location_constraint = bucket.location storage_class = bucket.storageClass fields = {'bucket': bucket_blr.url_string, 'storage_class': storage_class, 'location_constraint': location_constraint, 'acl': AclTranslation.JsonFromMessage(bucket.acl), 'default_acl': AclTranslation.JsonFromMessage( bucket.defaultObjectAcl)} fields['versioning'] = bucket.versioning and bucket.versioning.enabled fields['website_config'] = 'Present' if bucket.website else 'None' fields['logging_config'] = 'Present' if bucket.logging else 'None' fields['cors_config'] = 'Present' if bucket.cors else 'None' fields['lifecycle_config'] = 'Present' if bucket.lifecycle else 'None' # For field values that are multiline, add indenting to make it look # prettier. for key in fields: previous_value = fields[key] if (not isinstance(previous_value, basestring) or '\n' not in previous_value): continue new_value = previous_value.replace('\n', '\n\t ') # Start multiline values on a new line if they aren't already. if not new_value.startswith('\n'): new_value = '\n\t ' + new_value fields[key] = new_value print('{bucket} :\n' '\tStorage class:\t\t\t{storage_class}\n' '\tLocation constraint:\t\t{location_constraint}\n' '\tVersioning enabled:\t\t{versioning}\n' '\tLogging configuration:\t\t{logging_config}\n' '\tWebsite configuration:\t\t{website_config}\n' '\tCORS configuration: \t\t{cors_config}\n' '\tLifecycle configuration:\t{lifecycle_config}\n' '\tACL:\t\t\t\t{acl}\n' '\tDefault ACL:\t\t\t{default_acl}'.format(**fields)) if bucket_blr.storage_url.scheme == 's3': print('Note: this is an S3 bucket so configuration values may be ' 'blank. To retrieve bucket configuration values, use ' 'individual configuration commands such as gsutil acl get ' '<bucket>.')
def testAclChangeWithAllUsers(self): change = aclhelpers.AclChange('AllUsers:WRITE', scope_type=aclhelpers.ChangeType.GROUP) acl = list( AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) change.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'WRITER', 'AllUsers')
def testAclChangeWithProjectOwners(self): change = aclhelpers.AclChange(self._project_test_acl + ':READ', scope_type=aclhelpers.ChangeType.PROJECT) acl = list( AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) change.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'READER', 'Project', self._project_test_acl)
def testAclChangeWithDomain(self): change = aclhelpers.AclChange(self.DOMAIN_TEST + ':READ', scope_type=aclhelpers.ChangeType.GROUP) acl = list( AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) change.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'READER', 'GroupByDomain', self.DOMAIN_TEST)
def testAclChangeWithGroupEmail(self): change = aclhelpers.AclChange(self.GROUP_TEST_ADDRESS + ':fc', scope_type=aclhelpers.ChangeType.GROUP) acl = list( AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) change.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'OWNER', 'GroupByEmail', self.GROUP_TEST_ADDRESS)
def testAclChangeWithUserEmail(self): change = aclhelpers.AclChange(self.USER_TEST_ADDRESS + ':r', scope_type=aclhelpers.ChangeType.USER) acl = list( AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) change.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'READER', 'UserByEmail', self.USER_TEST_ADDRESS)
def testAclChangeWithGroupId(self): change = aclhelpers.AclChange(self.GROUP_TEST_ID + ':r', scope_type=aclhelpers.ChangeType.GROUP) acl = list( AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) change.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'READER', 'GroupById', self.GROUP_TEST_ID)
def testAclChangeWithAllAuthUsers(self): change = aclhelpers.AclChange('AllAuthenticatedUsers:READ', scope_type=aclhelpers.ChangeType.GROUP) acl = list(AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) change.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'READER', 'AllAuthenticatedUsers') remove = aclhelpers.AclDel('AllAuthenticatedUsers') remove.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHasNo(acl, 'READER', 'AllAuthenticatedUsers')
def testAclDelWithGroup(self): add = aclhelpers.AclChange(self.USER_TEST_ADDRESS + ':READ', scope_type=aclhelpers.ChangeType.GROUP) acl = list(AclTranslation.BotoBucketAclToMessage(self.sample_uri.get_acl())) add.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHas(acl, 'READER', 'GroupByEmail', self.USER_TEST_ADDRESS) remove = aclhelpers.AclDel(self.USER_TEST_ADDRESS) remove.Execute(self.sample_url, acl, 'acl', self.logger) self._AssertHasNo(acl, 'READER', 'GroupByEmail', self.GROUP_TEST_ADDRESS)
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.updated: print '\tCreation time:\t\t%s' % obj.updated.strftime( '%a, %d %b %Y %H:%M:%S GMT') if obj.cacheControl: print '\tCache-Control:\t\t%s' % obj.cacheControl if obj.contentDisposition: print '\tContent-Disposition:\t\t%s' % obj.contentDisposition if obj.contentEncoding: print '\tContent-Encoding:\t\t%s' % obj.contentEncoding if obj.contentLanguage: print '\tContent-Language:\t%s' % obj.contentLanguage print '\tContent-Length:\t\t%s' % obj.size print '\tContent-Type:\t\t%s' % obj.contentType if obj.componentCount: print '\tComponent-Count:\t%d' % obj.componentCount 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 '\tMetadata:' for ap in non_marker_props: meta_string = '\t\t%s:\t\t%s' % (ap.key, ap.value) print meta_string.encode(UTF8) if obj.crc32c: print '\tHash (crc32c):\t\t%s' % obj.crc32c if obj.md5Hash: print '\tHash (md5):\t\t%s' % obj.md5Hash print '\tETag:\t\t\t%s' % obj.etag.strip('"\'') if obj.generation: generation_str = GenerationFromUrlAndString(storage_url, obj.generation) print '\tGeneration:\t\t%s' % generation_str if obj.metageneration: print '\tMetageneration:\t\t%s' % 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 '\tACL:\t\t%s' % AclTranslation.JsonFromMessage(obj.acl) elif S3_ACL_MARKER_GUID in marker_props: print '\tACL:\t\t%s' % marker_props[S3_ACL_MARKER_GUID] else: print( '\tACL:\t\t\tACCESS DENIED. Note: you need OWNER ' 'permission\n\t\t\t\ton the object to read its ACL.') return (num_objs, num_bytes)
def _PrintBucketInfo(self, bucket_blr, listing_style): """Print listing info for given bucket. Args: bucket_blr: BucketListingReference for the bucket being listed listing_style: ListingStyle enum describing type of output desired. Returns: Tuple (total objects, total bytes) in the bucket. """ if (listing_style == ListingStyle.SHORT or listing_style == ListingStyle.LONG): print bucket_blr return # listing_style == ListingStyle.LONG_LONG: # We're guaranteed by the caller that the root object is populated. bucket = bucket_blr.root_object location_constraint = bucket.location storage_class = bucket.storageClass fields = {'bucket': bucket_blr.url_string, 'storage_class': storage_class, 'location_constraint': location_constraint, 'acl': AclTranslation.JsonFromMessage(bucket.acl), 'default_acl': AclTranslation.JsonFromMessage( bucket.defaultObjectAcl)} fields['versioning'] = bucket.versioning and bucket.versioning.enabled fields['website_config'] = 'Present' if bucket.website else 'None' fields['logging_config'] = 'Present' if bucket.logging else 'None' fields['cors_config'] = 'Present' if bucket.cors else 'None' fields['lifecycle_config'] = 'Present' if bucket.lifecycle else 'None' if bucket.labels: fields['labels'] = LabelTranslation.JsonFromMessage( bucket.labels, pretty_print=True) else: fields['labels'] = 'None' # Fields not available in all APIs (e.g. the XML API) if bucket.metageneration: fields['metageneration'] = bucket.metageneration if bucket.timeCreated: fields['time_created'] = bucket.timeCreated.strftime( '%a, %d %b %Y %H:%M:%S GMT') if bucket.updated: fields['updated'] = bucket.updated.strftime('%a, %d %b %Y %H:%M:%S GMT') # For field values that are multiline, add indenting to make it look # prettier. for key in fields: previous_value = fields[key] if (not isinstance(previous_value, basestring) or '\n' not in previous_value): continue new_value = previous_value.replace('\n', '\n\t ') # Start multiline values on a new line if they aren't already. if not new_value.startswith('\n'): new_value = '\n\t ' + new_value fields[key] = new_value # Only display certain properties if the given API returned them (JSON API # returns many fields that the XML API does not). metageneration_line = '' time_created_line = '' time_updated_line = '' if 'metageneration' in fields: metageneration_line = '\tMetageneration:\t\t\t{metageneration}\n' if 'time_created' in fields: time_created_line = '\tTime created:\t\t\t{time_created}\n' if 'updated' in fields: time_updated_line = '\tTime updated:\t\t\t{updated}\n' print(('{bucket} :\n' '\tStorage class:\t\t\t{storage_class}\n' '\tLocation constraint:\t\t{location_constraint}\n' '\tVersioning enabled:\t\t{versioning}\n' '\tLogging configuration:\t\t{logging_config}\n' '\tWebsite configuration:\t\t{website_config}\n' '\tCORS configuration: \t\t{cors_config}\n' '\tLifecycle configuration:\t{lifecycle_config}\n' '\tLabels:\t\t\t\t{labels}\n' + time_created_line + time_updated_line + metageneration_line + '\tACL:\t\t\t\t{acl}\n' '\tDefault ACL:\t\t\t{default_acl}').format(**fields)) if bucket_blr.storage_url.scheme == 's3': print('Note: this is an S3 bucket so configuration values may be ' 'blank. To retrieve bucket configuration values, use ' 'individual configuration commands such as gsutil acl get ' '<bucket>.')