Setting Object Properties
You can call ObsClient.setObjectMetadata to set object properties, including custom metadata.
In addition to standard HTTP headers, you can also customize metadata. Custom metadata can contain only letters, digits, and hyphens (-).
The following code shows how to set object properties:
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // Obtain an AK/SK pair on the management console. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an ObsClient instance. final ObsClient obsClient = new ObsClient(ak, sk, endPoint); SetObjectMetadataRequest request = new SetObjectMetadataRequest("bucketname", "objectname"); // Standard HTTP headers of an object request.setContentType("ContentType"); request.setExpires("Expires"); // Customize metadata. request.addUserMetadata("property1", "property-value1"); ObjectMetadata metadata = obsClient.setObjectMetadata(request); Log.i("\t" + metadata.getUserMetadata("property1"));
The following code shows how to set the expiration time of an object.
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
SetObjectMetadataRequest request = new SetObjectMetadataRequest("bucketname", "objectname");
HashMap<String, String> userHeaders = new HashMap<>();
// Set the expiration time of the object to five days.
userHeaders.put("x-obs-expires","5");
// Set the metadata storage method.
userHeaders.put("x-obs-metadata-directive","REPLACE_NEW");
request.setUserHeaders(userHeaders);
ObjectMetadata metadata = obsClient.setObjectMetadata(request);
Log.i("\t" + metadata.getUserMetadata("property1"));
- Due to HTTP coding restrictions, non-ASCII characters cannot be sent. If your request headers contain full-width characters, the SDK will URL encode these characters before sending the request. When you use a browser to access the object metadata, the browser automatically decodes the data.
- If you do not need the SDK to decode for you, call SetObjectMetadataRequest.setIsEncodeHeaders(false) to disable auto encoding.
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