Beispiel #1
0
def SerializeFileAttributesToObjectMetadata(posix_attrs,
                                            custom_metadata,
                                            preserve_posix=False):
    """Takes a POSIXAttributes object and serializes it into custom metadata.

  Args:
    posix_attrs: A POSIXAttributes object.
    custom_metadata: A custom metadata object to serialize values into.
    preserve_posix: Whether or not to preserve POSIX attributes other than
                    mtime.
  """
    # mtime will always be needed in the object metadata for rsync.
    if posix_attrs.mtime != NA_TIME:
        CreateCustomMetadata(entries={MTIME_ATTR: posix_attrs.mtime},
                             custom_metadata=custom_metadata)
    # Only add other POSIX attributes if the preserve_posix flag is set.
    if preserve_posix:
        if posix_attrs.atime != NA_TIME:
            CreateCustomMetadata(entries={ATIME_ATTR: posix_attrs.atime},
                                 custom_metadata=custom_metadata)
        if posix_attrs.uid != NA_ID:
            CreateCustomMetadata(entries={UID_ATTR: posix_attrs.uid},
                                 custom_metadata=custom_metadata)
        if posix_attrs.gid != NA_ID:
            CreateCustomMetadata(entries={GID_ATTR: posix_attrs.gid},
                                 custom_metadata=custom_metadata)
        if posix_attrs.mode.permissions != NA_MODE:
            CreateCustomMetadata(
                entries={MODE_ATTR: posix_attrs.mode.permissions},
                custom_metadata=custom_metadata)
Beispiel #2
0
 def SetPOSIXMetadata(self, provider, bucket_name, object_name, atime=None,
                      mtime=None, uid=None, gid=None, mode=None):
   """Sets POSIX metadata for the object."""
   obj_metadata = apitools_messages.Object()
   obj_metadata.metadata = apitools_messages.Object.MetadataValue(
       additionalProperties=[])
   if atime is not None:
     CreateCustomMetadata(entries={ATIME_ATTR: atime},
                          custom_metadata=obj_metadata.metadata)
   if mode is not None:
     CreateCustomMetadata(entries={MODE_ATTR: mode},
                          custom_metadata=obj_metadata.metadata)
   if mtime is not None:
     CreateCustomMetadata(entries={MTIME_ATTR: mtime},
                          custom_metadata=obj_metadata.metadata)
   if uid is not None:
     CreateCustomMetadata(entries={UID_ATTR: uid},
                          custom_metadata=obj_metadata.metadata)
   if gid is not None:
     CreateCustomMetadata(entries={GID_ATTR: gid},
                          custom_metadata=obj_metadata.metadata)
   if provider == 'gs':
     self.json_api.PatchObjectMetadata(bucket_name, object_name, obj_metadata,
                                       provider=provider)
   else:
     self.xml_api.PatchObjectMetadata(bucket_name, object_name, obj_metadata,
                                      provider=provider)
Beispiel #3
0
    def _SetObjectCustomMetadataAttribute(self, provider, bucket_name,
                                          object_name, attr_name, attr_value):
        """Sets a custom metadata attribute for an object.

    Args:
      provider: Provider string for the bucket, ex. 'gs' or 's3.
      bucket_name: The name of the bucket the object is in.
      object_name: The name of the object itself.
      attr_name: The name of the custom metadata attribute to set.
      attr_value: The value of the custom metadata attribute to set.

    Returns:
      None
    """
        obj_metadata = apitools_messages.Object()
        obj_metadata.metadata = CreateCustomMetadata({attr_name: attr_value})
        if provider == 'gs':
            self.json_api.PatchObjectMetadata(bucket_name,
                                              object_name,
                                              obj_metadata,
                                              provider=provider)
        else:
            self.xml_api.PatchObjectMetadata(bucket_name,
                                             object_name,
                                             obj_metadata,
                                             provider=provider)
Beispiel #4
0
  def CreateObjectJson(self, contents, bucket_name=None, object_name=None,
                       encryption_key=None, mtime=None, storage_class=None,
                       gs_idempotent_generation=None, kms_key_name=None):
    """Creates a test object (GCS provider only) using the JSON API.

    Args:
      contents: The contents to write to the object.
      bucket_name: Name of bucket to place the object in. If not specified,
          a new temporary bucket is created. Assumes the given bucket name is
          valid.
      object_name: The name to use for the object. If not specified, a temporary
          test object name is constructed.
      encryption_key: AES256 encryption key to use when creating the object,
          if any.
      mtime: The modification time of the file in POSIX time (seconds since
          UTC 1970-01-01). If not specified, this defaults to the current
          system time.
      storage_class: String representing the storage class to use for the
          object.
      gs_idempotent_generation: For use when overwriting an object for which
          you know the previously uploaded generation. Create GCS object
          idempotently by supplying this generation number as a precondition
          and assuming the current object is correct on precondition failure.
          Defaults to 0 (new object); to disable, set to None.
      kms_key_name: Fully-qualified name of the KMS key that should be used to
          encrypt the object. Note that this is currently only valid for 'gs'
          objects.

    Returns:
      An apitools Object for the created object.
    """
    bucket_name = bucket_name or self.CreateBucketJson().name
    object_name = object_name or self.MakeTempName('obj')
    preconditions = Preconditions(gen_match=gs_idempotent_generation)
    custom_metadata = apitools_messages.Object.MetadataValue(
        additionalProperties=[])
    if mtime is not None:
      CreateCustomMetadata({MTIME_ATTR: mtime}, custom_metadata)
    object_metadata = apitools_messages.Object(
        name=object_name,
        metadata=custom_metadata,
        bucket=bucket_name,
        contentType='application/octet-stream',
        storageClass=storage_class,
        kmsKeyName=kms_key_name)
    encryption_keywrapper = CryptoKeyWrapperFromKey(encryption_key)
    try:
      return self.json_api.UploadObject(
          cStringIO.StringIO(contents),
          object_metadata, provider='gs',
          encryption_tuple=encryption_keywrapper,
          preconditions=preconditions)
    except PreconditionException:
      if gs_idempotent_generation is None:
        raise
      with SetBotoConfigForTest([('GSUtil', 'decryption_key1',
                                  encryption_key)]):
        return self.json_api.GetObjectMetadata(bucket_name, object_name)