Downloading an Object - File-Based (SDK for Node.js)
If you have any questions during development, post them on the Issues page of GitHub.
Function
This API downloads an object as a file from OBS to your local computer.
Restrictions
- To download an object, you must be the bucket owner or have the required permission (obs:object:GetObject in IAM or GetObject in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
- To learn about the mappings between OBS regions and endpoints, see Regions and Endpoints.
- Objects in the Archive storage class can be downloaded only when they are in the Restored status.
Method
ObsClient.getObject(params)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
Bucket |
string |
Yes |
Explanation: Bucket name Restrictions:
Value range: The value can contain 3 to 63 characters. Default value: None |
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 of the object 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. Restrictions: None Value range: The value can contain 1 to 1,024 characters. Default value: None |
VersionId |
string |
No |
Explanation: Object version ID, for example, G001117FCE89978B0000401205D5DC9A Restrictions: None Value range: The value must contain 32 characters. Default value: None |
SaveAsFile |
string |
No |
Explanation: The download path with the file name contained Restrictions: A file name must be included in the path, for example, aa/bb.txt. Value range: None Default value: None |
SaveAsStream |
boolean |
No |
Explanation: Whether the object is returned as a readable stream Value range:
Default value: false |
ProgressCallback |
function |
No |
Explanation: Callback function for obtaining the download progress
NOTE:
This callback function contains the following parameters in sequence: number of downloaded bytes, total bytes, and used time (unit: second). Restrictions: None Value range: None Default value: None |
IfMatch |
string |
No |
Explanation: Preset ETag. If the ETag of the object to be downloaded is the same as the preset ETag, the object is returned. Otherwise, an error is returned. Restrictions: None Value range: The value must contain 32 characters. Default value: None |
IfModifiedSince |
string |
No |
Explanation: If the object was modified after the time specified by this parameter, its content is returned. Otherwise, an error code is returned. Restrictions: The value must conform with the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt. Value range: None Default value: None |
IfNoneMatch |
string |
No |
Explanation: Preset ETag. If the ETag of the object to be downloaded is different from the preset ETag, the object is returned. Otherwise, an error is returned. Restrictions: None Value range: The value must contain 32 characters. Default value: None |
IfUnmodifiedSince |
string |
No |
Explanation: If the object was modified before the time specified by this parameter, its content is returned. Otherwise, an error code is returned. Restrictions: The value must conform with the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt. Value range: None Default value: None |
Range |
string |
No |
Explanation: Download range. For example, 0-999 indicates the range is from byte 1 to byte 1000. Restrictions: The maximum length of Range is the length of the object minus 1. If the specified value exceeds this limit, the length of the object minus 1 is used. Value range: 0 to the object length minus 1. Format: bytes=x-y Default value: None |
Origin |
string |
No |
Explanation: Origin of the cross-domain request specified in the preflight request. It is usually a domain name. Restrictions: Each origin can contain at most one wildcard character (*). Value range: None Default value: None |
RequestHeader |
string |
No |
Explanation: HTTP headers that can be used in cross-origin requests. Only CORS requests matching the allowed headers are valid. Restrictions: Each header can contain only one wildcard character (*). Spaces, ampersands (&), colons (:), less-than signs (<), and full-width characters are not allowed. Value range: None Default value: None |
ResponseCacheControl |
string |
No |
Explanation: This parameter is used to rewrite the Cache-Control header in the response. Restrictions: None Value range: See the Cache-Control values defined in HTTP. Default value: None |
ResponseContentDisposition |
string |
No |
Explanation: This parameter is used to rewrite the Content-Disposition header in the response. Restrictions: None Value range: See the Content-Disposition values defined in HTTP. Default value: None |
ResponseContentEncoding |
string |
No |
Explanation: This parameter is used to rewrite the Content-Encoding header in the response. Restrictions: None Value range: See the Content-Encoding values defined in HTTP. Default value: None |
ResponseContentLanguage |
string |
No |
Explanation: This parameter is used to rewrite the Content-Language header in the response. Restrictions: None Value range: See the Content-Language values defined in HTTP. Default value: None |
ResponseContentType |
string |
No |
Explanation: This parameter is used to rewrite the Content-Type header in the response. Restrictions: None Value range: See What Is Content-Type (MIME)? (SDK for Node.js) Default value: None |
ResponseExpires |
string |
No |
Explanation: This parameter is used to rewrite the Expires header in the response. Restrictions: None Value range: See the Expires values defined in HTTP. Default value: None |
ImageProcess |
string |
No |
Explanation: Image processing command or style. Example for resizing and rotating an image: image/resize,m_fixed,w_100,h_100/rotate,90 Restrictions: None Value range: Command format: image/commands Style format: style/style-name For details about image processing parameters, see Processing Images. Default value: If no commands are entered, the original image is returned. |
SseC |
string |
Yes when SSE-C is used |
Explanation: SSE-C is used for decrypting objects. Restrictions: Only AES256 is supported. Value range: AES256 Default value: None |
SseCKey |
string |
Yes when SSE-C is used |
Explanation: Key used for decrypting objects when SSE-C is used Restrictions: A Base64-encoded, 256-bit value, for example, K7QkYpBkM5+hca27fsNkUnNVaobncnLht/rCB2o/9Cw= Value range: None Default value: None |
- If a download request includes IfUnmodifiedSince or IfMatch but does not meet the conditions specified by these parameters, an exception is thrown with HTTP status code 412 Precondition Failed.
- If a download request includes IfModifiedSince or IfNoneMatch but does not meet the conditions specified by these parameters, an exception is thrown with HTTP status code 304 Not Modified.
Responses
Type |
Description |
---|---|
NOTE:
This API returns a Promise response, which requires the Promise or async/await syntax. |
Explanation: Returned results. For details, see Table 3. |
Parameter |
Type |
Description |
---|---|---|
CommonMsg |
Explanation: Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 4. |
|
InterfaceResult |
Explanation: Results outputted for a successful call. For details, see Table 5. Restrictions: This parameter is not included if the value of Status is greater than 300. |
Parameter |
Type |
Description |
Status |
number |
Explanation: HTTP status code returned by the OBS server. Value range: A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes. |
Code |
string |
Explanation: Error code returned by the OBS server. |
Message |
string |
Explanation: Error description returned by the OBS server. |
HostId |
string |
Explanation: Request server ID returned by the OBS server. |
RequestId |
string |
Explanation: Request ID returned by the OBS server. |
Id2 |
string |
Explanation: Request ID2 returned by the OBS server. |
Indicator |
string |
Explanation: Error code details returned by the OBS server. |
Parameter |
Type |
Description |
---|---|---|
RequestId |
string |
Explanation: Request ID returned by the OBS server |
DeleteMarker |
string |
Explanation: Whether the deleted object is a delete marker. |
LastModified |
string |
Explanation: Time when the object was last modified. |
ContentLength |
string |
Explanation: Object size |
CacheControl |
string |
Explanation: Cache-Control header in the response. |
ContentDisposition |
string |
Explanation: Content-Disposition header in the response |
ContentEncoding |
string |
Explanation: Content-Encoding header in the response |
ContentLanguage |
string |
Explanation: Content-Language header in the response |
ContentType |
string |
Explanation: MIME type of the object |
Expires |
string |
Explanation: Expires header in the response |
ETag |
string |
Explanation: ETag of the object. |
VersionId |
string |
Explanation: Object version ID |
WebsiteRedirectLocation |
string |
Explanation: Location where an object is redirected. It is available when website hosting is configured for a bucket. |
StorageClass |
string |
Explanation: Object storage class. If the storage class is Standard, this parameter is left blank. |
Restore |
string |
Explanation: Restoration status of the object in the Archive storage class |
Expiration |
string |
Explanation: Expiration details |
Content |
string | stream.Readable |
Explanation: Content of the object. The content is left blank if SaveAsFile was specified. The content is a stream.Readable object if SaveAsStream was set to true. The content is a Buffer object if neither SaveAsFile nor SaveAsStream was specified. |
Metadata |
object |
Explanation: Custom metadata of the object. You need to add in the CORS configurations the additional headers allowed to be carried in the response. For example, you can add the x-amz-meta-property1 header to allow custom metadata property1 to be returned. |
Code Examples
This example downloads object examplebucket/objectname as a file.
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 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an instance of ObsClient. const obsClient = new ObsClient({ // 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use. server: "https://obs.ap-southeast-1.myhuaweicloud.com" }); async function getObject() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", // Specify the object to download (example/objectname in this example). Key: 'example/objectname', // Specify the destination path for the downloaded object. SaveAsFile: 'localfile' }; // Download an object as a file. const result = await obsClient.getObject(params); if (result.CommonMsg.Status <= 300) { console.log("Get object(%s) under the bucket(%s) successful!", params.Key, params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); return; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("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."); console.log(error); }; }; getObject(); |
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