By default, when you call the GetObject operation on an object in a versioning-enabled bucket, only the current version of the object is returned.
Background information
When you call the GetObject operation on a bucket, one of the following cases applies:
-
If the current version of the object is a delete marker, OSS returns 404 Not Found.
-
If you specify an object's versionId in the query parameter, the specified version is returned. If you set the versionId to "null", the object version that has a null versionId is returned.
-
If you try to retrieve a delete marker by specifying its versionId, OSS returns 405 Method Not Allowed.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.
|
API |
Action |
Description |
|
GetObject |
|
Downloads an object. |
|
|
When downloading an object, if you specify the object version through versionId, this permission is required. |
|
|
|
When downloading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, this permission is required. |
Sample code
The following sample code shows how to download a file:
const OSS = require('ali-oss');
const client = new OSS({
// Set yourregion to the region where the bucket is located. For example, for the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run this code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set yourbucketname to the name of the bucket.
bucket: 'yourbucketname'
});
async function get() {
// The name of the file.
const result = await client.get('filename', {
// The version ID of the file to download.
versionId: 'versionid',
});
console.log(result.content);
}
get();
References
For more information about the API operation used to download files, see GetObject.