本文介绍了如何在没有凭据的情况下将文件上传到s3公共存储桶中客户端javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想使用Javascript AWS开发工具包将文件上传到S3,但完全不使用凭证.使用凭据上传有效

We'd like to use Javascript AWS SDK to upload files to S3, but without using credentials at all. Uploading using credentials works

我们有一个存储桶,当我尝试上传此错误时,所有人都可以上传和下载文件的权限

We have a bucket which has the permissions of anyone that can upload and download files when i am trying to upload i'm getting this error

message: "No credentials to load"

我的代码:

var bucket = new AWS.S3({params: {Bucket: 'bucketName'}});

 var params = {Key: 'data.txt', Body: "kkkkkkkkkkkk"};

bucket.putObject(params, function (err, data) {
alert(err)

});

推荐答案

如果您已为所有用户设置了读/写权限,并且存储桶的cors配置允许PUT请求,则您应该可以使用ajax轻松完成此操作:

If you have set read/write permissions for all users and your bucket's cors configuration allows PUT requests you should be able to simply do it with ajax:

function httpReq(method, url, data, successCB, errorCB) {
    var xhr = new XMLHttpRequest();

    xhr.addEventListener('load', function () {
        if (xhr.status == 200) {
            successCB(xhr.response);
        } else {
            errorCB({status: xhr.status, response: xhr.response});
        }
    });

    xhr.open(method, url, true);
    xhr.send(data);
}

httpReq('PUT', 'http://yourBucket.s3.amazonaws.com/yourKey', file, function (response) {
    console.log('Upload Successful', response);
}, function (error) {
    console.error(error);
});

这篇关于如何在没有凭据的情况下将文件上传到s3公共存储桶中客户端javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:32