正确的代码将本地文件上传到API网关的S3代理

时间:2022-01-20 23:03:36

I created an API function to work with S3. I imported the template swagger. After deployment, I tested with a Node.js project by the npm module aws-api-gateway-client.

我创建了一个API函数来使用S3。我导入了模板招摇。部署之后,我通过npm模块aws-api-gateway-client使用Node.js项目进行了测试。

It works well with: get bucket lists, get bucket info, get one item, put a bucket, put a plain text object, however I am blocked with put a binary file.

它适用于:获取存储桶列表,获取存储桶信息,获取一个项目,放入存储桶,放置纯文本对象,但是我被阻止放入二进制文件。

firstly, I ensure ACL is allowed with all permissions on S3. secondly, binary support also added image/gif application/octet-stream

首先,我确保ACL具有S3的所有权限。其次,二进制支持还添加了image / gif application / octet-stream

正确的代码将本地文件上传到API网关的S3代理

The code snippet is as below. The behaviors are:

代码段如下。行为是:

1) after invokeAPI, the callback function is never hit, after sometime, the Node.js project did not respond. no any error message. The file size (such as an image) is very small. 2) with only two times, the uploading seemed to work, but the result file size is bigger (around 2M bigger) than the original file, so the file is corrupt.

1)在invokeAPI之后,回调函数永远不会被命中,经过一段时间后,Node.js项目没有响应。没有任何错误消息。文件大小(例如图像)非常小。 2)只有两次,上传似乎工作,但结果文件大小比原始文件大(大约2M),因此文件已损坏。

Could you help me out? Thank you!

你能救我吗?谢谢!

var filepathname = './items/';
var filename = 'image1.png';

fs.stat(filepathname+filename, function (err, stats) {

    var fileSize = stats.size ;

    fs.readFile(filepathname+filename,'binary',function(err,data){ 

        var len = data.length;
        console.log('file len' + len);

        var pathTemplate = '/my-test-bucket/' +filename ; 
        var method = 'PUT';

        var params = {
            folder: '',
            item:''
        };
        var additionalParams = {
            headers: {
             'Content-Type': 'application/octet-stream',
              //'Content-Type': 'image/gif',
             'Content-Length': len
           }
        };

        var result1 = apigClient.invokeApi(params,pathTemplate,method,additionalParams,data) 
        .then(function(result){
            //never hit :(
            console.log(result);
         }).catch( function(result){
              //never hit :(
             console.log(result);
        });;

    }); 

});

1 个解决方案

#1


1  

We encountered the same problem. API Gateway is meant for limited data (10MB as of now), limits shown here,

我们遇到了同样的问题。 API网关适用于有限的数据(截至目前为10MB),此处显示的限制,

http://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html

http://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html

Self Signed URL to S3:

S3的自签名URL:

Create an S3 self signed URL for POST from the lambda or the endpoint where you are trying to post.

从lambda或您尝试发布的端点为POST创建一个S3自签名URL。

How do I put object to amazon s3 using presigned url?

如何使用预签名网址将对象放入亚马逊s3?

Now POST the image directly to S3.

现在将图像直接发布到S3。

Presigned POST:

预定的POST:

Apart from posting the image if you want to post additional properties, you can post it in multi-form format as well.

除了要发布图像(如果要发布其他属性),还可以以多格式格式发布。

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property

If you want to process the file after delivering to S3, you can create a trigger from S3 upon creation and process with your Lambda or anypoint that need to process.

如果要在传送到S3后处理文件,则可以在创建时使用S3创建触发器,并使用您的Lambda或需要处理的任何点进行处理。

Hope it helps.

希望能帮助到你。

#1


1  

We encountered the same problem. API Gateway is meant for limited data (10MB as of now), limits shown here,

我们遇到了同样的问题。 API网关适用于有限的数据(截至目前为10MB),此处显示的限制,

http://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html

http://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html

Self Signed URL to S3:

S3的自签名URL:

Create an S3 self signed URL for POST from the lambda or the endpoint where you are trying to post.

从lambda或您尝试发布的端点为POST创建一个S3自签名URL。

How do I put object to amazon s3 using presigned url?

如何使用预签名网址将对象放入亚马逊s3?

Now POST the image directly to S3.

现在将图像直接发布到S3。

Presigned POST:

预定的POST:

Apart from posting the image if you want to post additional properties, you can post it in multi-form format as well.

除了要发布图像(如果要发布其他属性),还可以以多格式格式发布。

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property

If you want to process the file after delivering to S3, you can create a trigger from S3 upon creation and process with your Lambda or anypoint that need to process.

如果要在传送到S3后处理文件,则可以在创建时使用S3创建触发器,并使用您的Lambda或需要处理的任何点进行处理。

Hope it helps.

希望能帮助到你。