简体   繁体   English

使用S3将JSON文件放入AWS存储桶

[英]PUT JSON file to AWS bucket using S3

I'm writing a ROI calculator script at my current workplace. 我正在当前的工作场所编写ROI计算器脚本。 The calculator is designed to show how much someone would save going with our packages etc... 该计算器旨在显示使用我们的包裹等可以节省多少费用...

The form requires the person to fill out name/email/phone upon submission. 该表格要求该人在提交时填写姓名/电子邮件/电话。 That data gets parsed into JSON format and attempted to PUT to an Amazon AWS server. 该数据将解析为JSON格式,然后尝试将其放入Amazon AWS服务器。

I'm using the JavaScript SDK API to generate a pre-signed URL to use in my AJAX PUT request. 我正在使用JavaScript SDK API生成预签名URL,以在我的AJAX PUT请求中使用。 I have tried various parameters within my request but still receive a 403 error that says - "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403. 我在请求中尝试了各种参数,但仍收到403错误,该错误是-“对预检请求的响应未通过访问控制检查:所请求的资源上没有'Access-Control-Allow-Origin'标头。Origin'因此不允许访问“ null”。响应的HTTP状态码为403。

When I look into the response its like this - 当我调查响应时,它是这样的-

AccessForbidden CORSResponse: CORS is not enabled for this bucket. AccessForbidden CORSResponse:未为此存储桶启用CORS。

If i click on the actual signed url that I console.log I get this - 如果我单击我console.log的实际签名的URL,我会得到这个-

SignatureDoesNotMatch SignatureDoesNotMatch

The request signature we calculated does not match the signature you provided. 我们计算出的请求签名与您提供的签名不匹配。 Check your key and signing method. 检查您的密钥和签名方法。

I have checked with the team that monitors this bucket, the signature and secret key are correct, and they have CORS enabled on the bucket. 我已经与监视此存储桶的团队进行了核对,签名和密钥正确,并且在存储桶上启用了CORS。 This is driving me crazy! 这真让我抓狂!

I should also add that I have tried using only GET as well, with the same error. 我还应该补充一点,我也尝试过仅使用GET,并且存在相同的错误。

Here is the code of my presigned URL and AJAX request - 这是我的预签名URL和AJAX请求的代码-

var s3 = new AWS.S3 ({
    accessKeyId: 'xxxxxxxxxxxxxxxxx',
    secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
});

    var uploadPreSignedUrl = s3.getSignedUrl('putObject', {
    Bucket: 'bucket name',
    Key: 'path stuff here' + username variable + '.JSON',
    ACL: 'authenticated-read',
    ContentType: 'application/json'
});

$.ajax({
        type: "PUT",
        url: uploadPreSignedUrl,
        headers: {
            "Content-Type": "application/json"
        },
        data: JSON.stringify(userInfo),
        success: function (msg) {
            console.log(msg);
        },
        error: function (request, status, error) {
            console.log(status);
        }
    });

I was able to solve this... turns out the back-end team needed to enable CORS on the API Gateway AND the actual bucket itself. 我能够解决此问题...事实证明,需要在API网关和实际存储桶本身上启用CORS的后端团队。 My pre-signed URLs were working just fine, and the AJAX call was also correct. 我的预签名URL正常工作,并且AJAX调用也正确。 Hope this can help someone in the future. 希望这可以在将来对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS S3 Lambda事件-从同一存储桶中获取,更新然后放入JSON文件 - AWS S3 Lambda event - get, update, then put JSON file from same bucket 使用绝对路径将文件上传到 aws S3 存储桶 - Upload file to aws S3 bucket using absolute path 无法删除 aws s3 存储桶上的文件 - Cannot delete file on aws s3 bucket 有没有一种方法可以将数组转换为json文件并将其保存/发布到S3 bucket(aws)中? - Is there a way to convert array into json file and save/post it in S3 bucket(aws)? 如何使用 React 和使用 aws-sdk 预签名的 axios 将文件上传到 S3 存储桶? - How to upload a file to S3 bucket with axios using React and a presigned using aws-sdk? 使用SSH2和SFTPStream将文件从服务器流式传输到AWS S3存储桶 - Using SSH2 and SFTPStream to stream file from server to AWS S3 Bucket 使用 nodejs 从 AWS S3 存储桶下载图像文件,更新:12/12 - downloading image file from AWS S3 bucket using nodejs, UPDATE :12/12 AWS S3 预签名 url 上传返回 200 但文件不在使用 NodeJS 和节点获取的存储桶中 - AWS S3 presigned url upload returns 200 but the file is not in the bucket using NodeJS & node-fetch 来自AWS S3 Bucket的NodeJS下载文件 - NodeJS download file from AWS S3 Bucket 将csv文件直接从服务器上传到aws s3存储桶 - Upload csv file to aws s3 bucket directly from a server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM