Setting Object Properties
![](https://support.huaweicloud.com/intl/en-us/sdk-nodejs-devg-obs/public_sys-resources/notice_3.0-en-us.png)
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.
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 text-based, streaming, file-based, or multipart mode or when copying the object.
The following table describes object properties.
Property Name |
Description |
Default Value |
---|---|---|
Content-Length |
Indicates the object length. If the object length exceeds the flow 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 |
Base64-encoded MD5 value of the object data. It is provided for the OBS server to verify data integrity. |
None |
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. |
None |
Customized metadata |
Indicates the user-defined description of the object. It is used to facilitate the customized management on the object. |
None |
Setting the Length for an Object
// Import the OBS library. // Use npm to install the client. var ObsClient = require('esdk-obs-nodejs'); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an ObsClient instance. var obsClient = new ObsClient({ //Obtain an AK/SK pair using environment variables or import the 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, server : 'https://your-endpoint' }); obsClient.putObject({ Bucket : 'bucketname', Key : 'objectname', SourceFile : 'localfile', ContentLength : 1024 * 1024 // 1 MB }, (err, result) => { if(err){ console.error('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); } });
![](https://support.huaweicloud.com/intl/en-us/sdk-nodejs-devg-obs/public_sys-resources/note_3.0-en-us.png)
Use the ContentLength parameter to specify the object length.
Setting the MIME Type for an Object
// Import the OBS library. // Use npm to install the client. var ObsClient = require('esdk-obs-nodejs'); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an ObsClient instance. var obsClient = new ObsClient({ //Obtain an AK/SK pair using environment variables or import the 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, server : 'https://your-endpoint' }); // Upload an image. obsClient.putObject({ Bucket : 'bucketname', Key : 'objectname.jpg', SourceFile : 'localimage.jpg', ContentType : 'image/jpeg' }, (err, result) => { if(err){ console.error('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); } });
![](https://support.huaweicloud.com/intl/en-us/sdk-nodejs-devg-obs/public_sys-resources/note_3.0-en-us.png)
- Use the ContentType parameter to set the MIME type for an object.
- If this property is not specified, the SDK will automatically identify the MIME type according to the suffix of the object name. For example, if the suffix of the object name is .xml (.html), the object will be identified as an application/xml (text/html) file.
Setting the MD5 Value for an Object
// Import the OBS library. // Use npm to install the client. var ObsClient = require('esdk-obs-nodejs'); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an ObsClient instance. var obsClient = new ObsClient({ //Obtain an AK/SK pair using environment variables or import the 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, server : 'https://your-endpoint' }); obsClient.putObject({ Bucket : 'bucketname', Key : 'objectname', SourceFile : 'localimage.jpg', ContentMD5 : 'your md5 which should be encoded by base64' }, (err, result) => { if(err){ console.error('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); } });
![](https://support.huaweicloud.com/intl/en-us/sdk-nodejs-devg-obs/public_sys-resources/note_3.0-en-us.png)
- Use the ContentMD5 parameter to specify the MD5 value for an object.
- 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.
Setting the Storage Class for an Object
// Import the OBS library. // Use npm to install the client. var ObsClient = require('esdk-obs-nodejs'); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an ObsClient instance. var obsClient = new ObsClient({ //Obtain an AK/SK pair using environment variables or import the 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, server : 'https://your-endpoint' }); obsClient.putObject({ Bucket : 'bucketname', Key : 'objectname', SourceFile : 'localfile', // Set the storage class to Archive. StorageClass : ObsClient.enums.StorageClassCold }, (err, result) => { if(err){ console.error('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); } });
![](https://support.huaweicloud.com/intl/en-us/sdk-nodejs-devg-obs/public_sys-resources/note_3.0-en-us.png)
- Use the StorageClass parameter to set the storage class for an object.
- If you do not set the storage class for an object, the storage class of the object will be the same as that of its residing 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.
Customizing Metadata for an Object
// Import the OBS library. // Use npm to install the client. var ObsClient = require('esdk-obs-nodejs'); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an ObsClient instance. var obsClient = new ObsClient({ //Obtain an AK/SK pair using environment variables or import the 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, server : 'https://your-endpoint' }); obsClient.putObject({ Bucket : 'bucketname', Key : 'objectname', SourceFile : 'localfile', Metadata : {'property1':'property-value1', 'property2' : 'property-value2'}, }, (err, result) => { if(err){ console.error('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); } });
![](https://support.huaweicloud.com/intl/en-us/sdk-nodejs-devg-obs/public_sys-resources/note_3.0-en-us.png)
- Use the Metadata parameter to customize the metadata of an object.
- 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. The total metadata 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.
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