Configuring Object Metadata (SDK for Go)
Function
Object metadata is a set of name-value pairs that describe the object and is used for object management.
Currently, only the metadata defined by the system is supported.
There are two kinds of metadata: system-controlled metadata and user-controlled metadata. For example, metadata such as Last-Modified is controlled by the system and cannot be modified. You can modify the metadata such as ContentLanguage through the API.
This API sends a HEAD request to configure the metadata of an object in a specified bucket.
Restrictions
- To configure object metadata, you must be the bucket owner or have the required permission (obs:object:ModifyObjectMetaData in IAM or ModifyObjectMetaData in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
- When versioning is enabled for a bucket, you can set metadata for objects of the latest version, but cannot set metadata for objects of historical versions.
- You cannot set metadata for Archive objects.
Method
func (obsClient ObsClient) SetObjectMetadata(input *SetObjectMetadataInput) (output *SetObjectMetadataOutput, err error)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
input |
Yes |
Explanation: Input parameters for configuring object metadata. For details, see Table 2. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
Bucket |
string |
Yes |
Explanation: Bucket name Restrictions:
Value range: The value must contain 3 to 63 characters. |
Key |
string |
Yes |
Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. Value range: The value must contain 1 to 1,024 characters. Default value: None |
VersionId |
string |
No |
Explanation: Object version ID, for example, G001117FCE89978B0000401205D5DC9A Value range: The value must contain 32 characters. Default value: None |
CacheControl |
string |
No |
Explanation: Cache-Control header in the response. It specifies cache behaviors of the web page when an object is downloaded. Default value: None |
ContentDisposition |
string |
No |
Explanation: Content-Disposition header in the response. It specifies the name of the object when it is downloaded. Default value: None |
ContentEncoding |
string |
No |
Explanation: Content-Encoding header in the response. It specifies the content encoding format when an object is downloaded. Default value: None |
ContentLanguage |
string |
No |
Explanation: Content-Language header in the response. It specifies the content language format when an object is downloaded. Default value: None |
ContentType |
string |
No |
Explanation: Content-Type header in the response. It specifies the object file type. Default value: None |
Expires |
string |
No |
Explanation: Expires header in the response. It specifies the cache expiration time of the web page when the object is downloaded. Default value: None |
WebsiteRedirectLocation |
string |
No |
Explanation: If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL. The request is redirected to object anotherPage.html in the same bucket: WebsiteRedirectLocation:/anotherPage.html The request is redirected to an external URL http://www.example.com/: WebsiteRedirectLocation:http://www.example.com/ OBS obtains the specified value from the header and stores it in the object metadata WebsiteRedirectLocation. Restrictions:
Default value: None |
StorageClass |
No |
Explanation: Storage class of an object. Value range: See Table 3. Default value: None |
|
Metadata |
map[string]string |
No |
Explanation: Custom metadata of the object. You can add a header starting with x-obs-meta- in the request to define metadata. The custom metadata will be returned in the response when you retrieve the object or query the object metadata. Restrictions:
Default value: None |
Constant |
Default Value |
Description |
---|---|---|
StorageClassStandard |
STANDARD |
OBS Standard Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response. |
StorageClassWarm |
WARM |
OBS Infrequent Access Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed. |
StorageClassCold |
COLD |
OBS Archive Used for storing rarely accessed (once a year) data. |
Responses
Parameter |
Type |
Description |
---|---|---|
output |
Explanation: Returned results. For details, see Table 5. |
|
err |
error |
Explanation: Error messages returned by the API |
Parameter |
Type |
Description |
---|---|---|
StatusCode |
int |
Explanation: HTTP status code Value range: A status code is a group of digits that can be 2xx (indicating successes) or 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code. Default value: None |
RequestId |
string |
Explanation: Request ID returned by the OBS server Default value: None |
ResponseHeaders |
map[string][]string |
Explanation: HTTP response headers Default value: None |
MetadataDirective |
Explanation: Policy for copying the source object's properties Value range: See Table 6. Default value: None |
|
CacheControl |
string |
Explanation: Cache-Control header in the response. It specifies cache behaviors of the web page when an object is downloaded. Default value: None |
ContentDisposition |
string |
Explanation: Content-Disposition header in the response. It specifies the name of the object when it is downloaded. Default value: None |
ContentEncoding |
string |
Explanation: Content-Encoding header in the response. It specifies the content encoding format when an object is downloaded. Default value: None |
ContentLanguage |
string |
Explanation: Content-Language header in the response. It specifies the content language format when an object is downloaded. Default value: None |
ContentType |
string |
Explanation: Content-Type header in the response. It specifies the object file type. Default value: None |
Expires |
string |
Explanation: Expires header in the response. It specifies the cache expiration time of the web page when the object is downloaded. Default value: None |
WebsiteRedirectLocation |
string |
Explanation: If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL. The request is redirected to object anotherPage.html in the same bucket: WebsiteRedirectLocation:/anotherPage.html The request is redirected to an external URL http://www.example.com/: WebsiteRedirectLocation:http://www.example.com/ OBS obtains the specified value from the header and stores it in the object metadata WebsiteRedirectLocation. Restrictions:
Default value: None |
StorageClass |
Explanation: Object storage class. If you do not specify this header, the object inherits the storage class of the bucket. Value range: See Table 7. Default value: None |
|
Metadata |
map[string]string |
Explanation: Custom metadata of the object. You can add a header starting with x-obs-meta- in the request to define metadata. The custom metadata will be returned in the response when you retrieve the object or query the object metadata. Restrictions:
Default value: None |
Constant |
Default Value |
Description |
---|---|---|
CopyMetadata |
COPY |
When copying an object, the object's properties are also copied. |
ReplaceMetadata |
REPLACE |
When copying an object, the object's properties are replaced. |
Constant |
Default Value |
Description |
---|---|---|
StorageClassStandard |
STANDARD |
OBS Standard Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response. |
StorageClassWarm |
WARM |
OBS Infrequent Access Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed. |
StorageClassCold |
COLD |
OBS Archive Used for storing rarely accessed (once a year) data. |
Code Examples
This example configures metadata for object example/objectname in bucket examplebucket. The MIME type of the object is set to image/jpeg, the storage class is set to Archive, and custom metadata is also configured for the object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { //Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // securityToken := os.Getenv("SecurityToken") // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com" // Create an obsClient instance. // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance. obsClient, err := obs.New(ak, sk, endPoint /*, obs.WithSecurityToken(securityToken)*/) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } input := &obs.SetObjectMetadataInput{} // Specify a bucket name. input.Bucket = "examplebucket" // Specify an object (example/objectname as an example). input.Key = "example/objectname" // Specify a MIME type (image/jpeg as an example) for the object. input.ContentType = "image/jpeg" // Specify a storage class (obs.StorageClassCold as an example) for the object. input.StorageClass = obs.StorageClassCold // Specify custom metadata. input.Metadata = map[string]string{"property1": "property-value1", "property2": "property-value2"} // Configure metadata for the object. output, err := obsClient.SetObjectMetadata(input) if err == nil { fmt.Printf("Set Object(%s)'s metadata successful with bucket(%s)!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Set Object(%s)'s metadata fail with bucket(%s)!\n", input.Key, input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.") fmt.Println(err) } } |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot