edit-icon download-icon

FAQ

Last Updated: Aug 08, 2018

How to call STS

A browser is an untrusted environment. It brings an extremely high risk if you store an AccessKeyID and AccessKeySecret in a browser. We recommend that you use the STS mode for OSS interface calls in a browser environment.

After obtaining the STS token, you can initialize the SDK.

  1. <script type="text/javascript">
  2. $.ajax("http://your_sts_server/",{method: 'GET'},function (err, result) {
  3. var client = new OSS.Wrapper({
  4. accessKeyId: result.AccessKeyId,
  5. accessKeySecret: result.AccessKeySecret,
  6. stsToken: result.SecurityToken,
  7. endpoint: '<oss endpoint>',
  8. bucket: '<Your bucket name>'
  9. });
  10. });
  11. </script>

How to enable HTTPS access

When you initialize the SDK, you can input the following parameters:

  • region: This parameter is the region used when you apply for the OSS service, such as oss-cn-hangzhou. Complete region lists can be viewed in Regions and endpoints.
  • internal: Used in combination with region. If internal is set to true, visit the intranet node.
  • secure: Used in combination with region. If secure is set to true, use the HTTPS for access.
  • endpoint: For example, http://oss-cn-hangzhou.aliyuncs.com. If endpoint is specified, the region is ignored. You can specify the HTTPS or IP address when using endpoint.

How to solve the cross-domain issues when using a browser

Set CORS attributes of buckets before using SDK in a browser. For more information, see bucket settings in Quick start.

How to set the user-defined data (meta), file type (mime), and request header (header) of the file to be uploaded

For more information, see Multipart upload.

Instruction for resumable upload on browser side

Save the checkpoint to a browser localStorage. You can directly use the checkpoint parameter next time to achieve resumable upload.

How to obtain the upload progress

You can obtain upload process when using multipart upload. For more information, see Upload objects.

Note that the progress parameter can be a generator function(function*), or a “thunk”:

  1. var progress = function (p) {
  2. return function (done) {
  3. console.log(p);
  4. done();
  5. };
  6. };

How to obtain the download progress

The browser does not provide download progress. You can call the signatureUrl method to obtain the download address. For more information, see Download objects.

How to upload files to a specified directory

Add the specified directory prefix to the object name to be uploaded. For more information, see Comparison between OSS and file system.

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. var result = yield client.multipartUpload('base-dir/' +'object-key', 'local-file', {
  11. progress: function* (p) {
  12. console.log('Progress: ' + p);
  13. }
  14. });
  15. console.log(result);
  16. }).catch(function (err) {
  17. console.log(err);
  18. });

How to upload a Base64 encoded image

Base64 first transcodes images into specified formats, and then calls OSS Upload interface to upload the images. For more information, see Github examples.

  1. /**
  2. * base64 to file
  3. * @param dataurl base64 content
  4. * @param filename set up a meaningful suffix, or you can set mime type in options
  5. * @returns {File|*}
  6. */
  7. const dataURLtoFile = function dataURLtoFile(dataurl, filename) {
  8. const arr = dataurl.split(',');
  9. const mime = arr[0].match(/:(.*?);/)[1];
  10. const bstr = atob(arr[1]);
  11. let n = bstr.length;
  12. const u8arr = new Uint8Array(n);
  13. while (n--) {
  14. u8arr[n] = bstr.charCodeAt(n);
  15. }
  16. return new Blob([u8arr], { type: mime });// if env support File, also can use this: return new File([u8arr], filename, { type: mime });
  17. };
  18. // client indicates OSS client instances
  19. const uploadBase64Img = function uploadBase64Img(client) {
  20. // base64 format content
  21. const base64Content = 'data:image:xxxxxxxxxxxxx';
  22. const filename = 'img.png';
  23. const imgfile = dataURLtoFile(base64Content, filename);
  24. //key indicates uploaded object key and imgFile indicates the returned images after processing by dataURLtoFile
  25. client.multipartUpload(key, imgfile).then((res) => {
  26. console.log('upload success: %j', res);
  27. }).catch((err) => {
  28. console.error(err);
  29. });
  30. };

How to limit the size of the uploaded file

The size (in bytes) of the uploaded file can be obtained in the browser according to document.getElementById(“file”).files[0].size. See the web post request.

How to obtain the signed URL of an object

You can call the signatureUrl method to obtain the download address. For more information, see Download objects.

How to use a signed URL generated by SDK to upload resources

Signed URLs are often used to authorize third parties to download and upload resources. The signatureUrl API provided by SDK is used to return a signed URL and users can use this URL to upload or download resources directly. For more information about how to upload resources using signed URLs, see Upload resources using signed URLs.

How to upload resources to OSS servers using Form-based File Upload

For more information, see Overview of direct transfer on Web client.

Common errors

Thank you! We've received your feedback.