Setting Object Properties

API Description

You can set properties for an object when uploading it. Object properties include the object length, MIME type, MD5 value (for verification), storage class, and customized metadata. You can set properties for an object that is being uploaded in streaming, file-based, or multipart mode or when copying an object.

The following table describes object properties.

Property Name

Description

Default Value

Content-Length

Indicates the object length. If the object length exceeds the stream or file length, the object will be truncated.

Actual length of the stream or file

Content-Type

Indicates the MIME type of the object, which defines the type and network code of the object as well as in which mode and coding will the browser read the object.

binary/octet-stream

Content-MD5

Indicates the base64-encoded digest of the object data. It is provided for the OBS server to verify data integrity.

N/A

Storage class

Indicates the storage class of the object. Different storage classes meet different needs for storage performance and costs. The value defaults to be the same as the object's residing bucket and can be changed.

N/A

Customized metadata

Indicates the user-defined description of the object. It is used to facilitate the customized management on the object.

N/A

Method Definition

func (obsClient ObsClient) PutFile(input *PutFileInput) (output *PutObjectOutput, err error)

Method Definition If a Signed URL Is Used

func (obsClient ObsClient) PutFileWithSignedUrl(signedUrl string, actualSignedRequestHeaders http.Header, sourceFile string)) (output *PutObjectOutput, err error)

Request Parameters

Field

Type

Optional or Mandatory

input

*PutFileInput

Mandatory

Returned Results

Field

Type

output

*PutObjectOutput

err

error

Sample Code

You can call PutObjectInput.ContentLength or PutFileInput.ContentLength to set the object length. Sample code:

func main() {
       input := &obs.PutFileInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       input.SourceFile = "localfile"
       // Only 100 bytes of the object will be uploaded.
       input.ContentLength = 100
       output, err := obsClient.PutFile(input)
       if err == nil {
              fmt.Printf("RequestId:%s\n", output.RequestId)
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}

You can call PutObjectInput.ContentType or PutFileInput.ContentType to set the MIME type for an object. Sample code is as follows:

func main() {
       input := &obs.PutFileInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       input.SourceFile = "localimage.jpg"
       // Upload an image.
       input.ContentType = "image/jpeg"
       output, err := obsClient.PutFile(input)
       if err == nil {
              fmt.Printf("RequestId:%s\n", output.RequestId)
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}

If this property is not specified, the SDK will automatically identify the MIME type according to the name suffix of the uploaded object. For example, if the suffix of the file is .xml (.html), the object will be identified as an application/xml (text/html) file.

You can call PutObjectInput.ContentMD5 or PutFileInput.ContentMD5 to set the MD5 value for an object. Sample code is as follows:

func main() {
       input := &obs.PutFileInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       input.SourceFile = "localfile"
       // Set the MD5 value.
       input.ContentMD5 = "your md5 which should be encoded by base64"
       output, err := obsClient.PutFile(input)
       if err == nil {
              fmt.Printf("RequestId:%s\n", output.RequestId)
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}
  • The MD5 value of an object must be a base64-encoded digest.
  • The OBS server will compare this MD5 value with the MD5 value obtained by object data calculation. If the two values are not the same, the upload fails with an HTTP 400 error returned.
  • If the MD5 value is not specified, the OBS server will skip MD5 value verification.

You can call PutObjectInput.StorageClass or PutFileInput.StorageClass to set the storage class for an object. Sample code:

func main() {
       input := &obs.PutFileInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       input.SourceFile = "localfile"
       // Set the storage class to OBS Archive.
       
       input.StorageClass = obs.StorageClassCold
       output, err := obsClient.PutFile(input)
       if err == nil {
              fmt.Printf("RequestId:%s\n", output.RequestId)
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}
  • The storage class of the objects in a bucket is the same as that of the bucket.
  • OBS provides objects with three storage classes which are consistent with those provided for buckets.
  • Before downloading an Archive object, you must restore it.

You can use the PutObjectInput.Metadata or PutFileInput.Metadata parameter to customize the metadata for the object. Sample code:

func main() {
       input := &obs.PutObjectInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       input.Body = strings.NewReader("Hello OBS")
       input.Metadata = map[string]string{"property1": "property-value1", "property2": "property-value2"}
       output, err := obsClient.PutObject(input)
       if err == nil {
              fmt.Printf("RequestId:%s\n", output.RequestId)
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}
  • In the preceding code, two pieces of metadata named property1 and property2 are customized and their respective values are set to property-value1 and property-value2.
  • An object can have multiple pieces of metadata whose total size cannot exceed 8 KB.
  • The customized object metadata can be obtained by using ObsClient.GetObjectMetadata. For details, see Obtaining Object Metadata.
  • When you call ObsClient.GetObject to download an object, its customized metadata will also be downloaded.
  • Currently, the metadata name cannot contain non-ASCII characters. If the metadata value contains non-ASCII characters, Base64 encoding is required.