This topic provides answers to some frequently asked questions when you use OSS SDK for Node.js.
What do I do if the "The region must be conform to the specifications" error message is returned?
This error occurs because the Region parameter is invalid. Check whether the value of the Region parameter is valid. For more information about region IDs, see Regions and endpoints.
How do I enable HTTPS access?
To enable HTTPS access, set secure to true when you initialize OSS SDK for Node.js.
How do I obtain the upload progress?
You can use the progress parameter to obtain the upload progress when you perform a multipart upload (Node.js SDK).
How do I obtain the download progress?
You can calculate the progress in OSS SDK for Node.js based on the size of the download traffic.
How do I upload Base64-encoded images?
Convert the images to File objects and upload them to the OSS server by calling the corresponding API operation.
function dataURLtoFile(dataurl, filename) {
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
let file = dataURLtoFile('base64 content', '');
client.multipartUpload('oss file name', file).then( (res)=> {
console.log(res)
}).catch((err) => {
console.log(err)
});
How do I upload an object to a specified directory?
You only need to add the prefix of the directory to the name of the object that you want to upload. For more information, see Comparison between OSS and file systems.
const OSS = require("ali-oss");
const client = new OSS({
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run this sample code, make sure 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,
// Specify the bucket name.
bucket: 'yourbucketname'
});
client
.multipartUpload("base-dir/" + "object-name", "local-file", {
progress: async function (p) {
console.log("Progress: " + p);
},
})
.then((result) => {
console.log(result);
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});How do I generate a signed URL to upload an object?
If you use the PUT method to upload an object, you must generate a signed URL. For more information, see Use a signed URL for temporary access authorization.
Signed URLs are not supported for object upload using the POST method. You can construct a POST request to upload objects. When you construct a POST request, you must configure the Key, OSSAccessKeyId, policy, and Signature parameters in the order specified in the API operation. For more information about the operation, see PostObject.
How do I obtain the signed URL of an object?
You can call the signatureUrl method to obtain the download URL. For more information, see the GitHub example.
Error when downloading an object larger than 1 GB: Response timeout for 60000ms, please increase the timeout or use multipartDownload.
The timeout period for OSS SDK for Node.js to perform download tasks is 60000 ms. If a timeout error occurs when you download an object greater than 1 GB, perform streaming download to download the object in increments and appropriately extend the timeout period. For more information about streaming download, see Streaming download (Node.js SDK). For more information about the timeout parameter, see Parameters.