def get_object_resource_from_metadata(metadata):
    """Helper method to generate a ObjectResource instance from GCS metadata.

  Args:
    metadata (messages.Object): Extract resource properties from this.

  Returns:
    ObjectResource with properties populated by metadata.
  """
    if metadata.generation is not None:
        # Generation may be 0 integer, which is valid although falsy.
        generation = str(metadata.generation)
    else:
        generation = None
    url = storage_url.CloudUrl(scheme=storage_url.ProviderPrefix.GCS,
                               bucket_name=metadata.bucket,
                               object_name=metadata.name,
                               generation=generation)

    if metadata.customerEncryption:
        key_hash = metadata.customerEncryption.keySha256
    else:
        key_hash = None

    return gcs_resource_reference.GcsObjectResource(
        url,
        content_type=metadata.contentType,
        creation_time=metadata.timeCreated,
        decryption_key_hash=key_hash,
        etag=metadata.etag,
        crc32c_hash=metadata.crc32c,
        md5_hash=metadata.md5Hash,
        metadata=metadata,
        metageneration=metadata.metageneration,
        size=metadata.size)
예제 #2
0
  def test_execute_translates_display_detail_to_fields_scope_for_non_buckets(
      self, display_detail, fields_scope, mock_wildcard_iterator):
    """Should translate cloud_list_task arg to cloud_api arg."""
    cloud_url = storage_url.storage_url_from_string('gs://bucket1/object1')
    # Will need get_metadata_dump for DisplayDetail.FULL.
    self.object1 = gcs_resource_reference.GcsObjectResource(
        cloud_url, metadata=self.messages.Object(name='o'))
    mock_wildcard_iterator.return_value = [self.object1]

    task = cloud_list_task.CloudListTask(
        cloud_url, display_detail=display_detail)
    task.execute()

    mock_wildcard_iterator.assert_called_once_with(
        cloud_url, fields_scope=fields_scope)
예제 #3
0
def _object_resource_from_metadata(metadata):
  """Helper method to generate a ObjectResource instance from GCS metadata.

  Args:
    metadata (messages.Object): Extract resource properties from this.

  Returns:
    ObjectResource with properties populated by metadata.
  """
  url = storage_url.CloudUrl(
      scheme=storage_url.ProviderPrefix.GCS,
      bucket_name=metadata.bucket,
      object_name=metadata.name,
      generation=getattr(metadata, 'generation', None))
  return gcs_resource_reference.GcsObjectResource(
      url, creation_time=metadata.timeCreated, etag=metadata.etag,
      metadata=metadata, size=metadata.size)
예제 #4
0
  def test_full_display_detail_prints_objects_properly(self, client):
    """Test objects are shown with formatted metadata."""
    object1 = gcs_resource_reference.GcsObjectResource(
        storage_url.storage_url_from_string('gs://bucket1/object1'),
        metadata=self.messages.Object(name='o'))
    client.get_bucket.side_effect = [self.bucket1]
    client.list_objects.side_effect = [[object1, self.dir1, self.dir2]]

    task = cloud_list_task.CloudListTask(
        storage_url.storage_url_from_string('gs://bucket1/'),
        display_detail=cloud_list_task.DisplayDetail.FULL)
    task.execute()

    output = self.GetOutput()
    expected_output = textwrap.dedent(
        """\
        [
        {
          "url": "gs://bucket1/object1",
          "type": "cloud_object",
          "metadata": {
            "name": "o"
          }
        },
        {
          "url": "gs://bucket1/dir1/",
          "type": "prefix"
        },
        {
          "url": "gs://bucket1/dir2/",
          "type": "prefix"
        }
        ]
        """
    )
    self.assertEqual(output, expected_output)

    client.get_bucket.assert_called_once_with(
        self.bucket1.name, cloud_api.FieldsScope.FULL)
    client.list_objects.assert_called_once_with(
        all_versions=False, bucket_name=self.bucket1.name,
        delimiter='/', fields_scope=cloud_api.FieldsScope.FULL, prefix=None)
    def test_dumps_object_metadata_with_nested_objects_in_correct_format(self):
        time = datetime.datetime(1111, 1, 1)
        messages = apis.GetMessagesModule('storage', 'v1')
        apitools_customer_encryption = messages.Object.CustomerEncryptionValue(
            encryptionAlgorithm='md6')
        apitools_object = messages.Object(
            name='object',
            generation=0,
            acl=[messages.ObjectAccessControl(bucket='bucket')],
            timeCreated=time,
            customerEncryption=apitools_customer_encryption,
            eventBasedHold=False,
        )
        resource = gcs_resource_reference.GcsObjectResource(
            storage_url.CloudUrl(storage_url.ProviderPrefix.GCS,
                                 bucket_name='bucket'),
            metadata=apitools_object)

        expected_dump = textwrap.dedent("""\
    {
      "url": "gs://bucket",
      "type": "cloud_object",
      "metadata": {
        "acl": [
          {
            "bucket": "bucket"
          }
        ],
        "customerEncryption": {
          "encryptionAlgorithm": "md6"
        },
        "eventBasedHold": false,
        "generation": 0,
        "name": "object",
        "timeCreated": "1111-01-01T00:00:00"
      }
    }""")
        self.assertEqual(resource.get_metadata_dump(), expected_dump)