如何将png文件上传到S3存储桶? [英] How to upload a png file to S3 Bucket?
问题描述
尝试使用 S3-for-Google-上传png
文件将Apps-Script 库添加到S3存储桶:
Trying to upload a png
file using S3-for-Google-Apps-Script library to S3 bucket:
// get the image blob
const imgBlob = UrlFetchApp.fetch('imageUrl').getBlob();
// init S3 instance
const s3 = S3.getInstance(awsAccessKeyId, awsSecretKey);
// upload the image to S3 bucket
s3.putObject(bucketName, 'test.png', imgBlob, { logRequests:true });
文件正在上传到S3,但不是完美的方式!看起来像这样:
The file is uploading to S3 but not in a perfect way! It looks like this:
如果我下载图像并打开获取error
:
If I download the image and open getting the error
:
它可能已损坏或使用了预览无法识别的文件格式."
"It may be damaged or use a file format that Preview doesn’t recognize."
那么,如何将.png
文件上传到亚马逊S3存储桶?
So, how can I upload a .png
file to amazon S3 bucket?
当将'base64'用于s3.putObject()
时,我可以正确上传图像:
I can correctly upload the image when 'base64' is used to s3.putObject()
:
const base64 = Utilities.base64Encode(imgBlob.getBytes());
s3.putObject(bucketName, 'test.png', base64, { logRequests:true });
// go to S3 and clicking on the link I can see the base64 string
但这是作为String
上传的,例如当我去S3&单击test.png
,我会看到类似的内容:"iVBORw0KGgoAAAANSUhEUgAAAgAAAAI ... II="
,但是我想查看实际的图像,而不是String
.
But this is uploading as String
e.g. when I go S3 & click on test.png
I see something like this: "iVBORw0KGgoAAAANSUhEUgAAAgAAAAI ... II="
, but I want to see the actual image, not a String
.
推荐答案
我相信您的情况和目标如下.
I believe your situation and goal as follows.
- 根据您的情况,可以上传图像的base64数据.但是,上传的数据不是图像.这是字符串数据.
- 您的目标是要使用图像URL上传公共共享图像的图像文件.
为此,这个答案如何?
当我看到"S3-for-Google-Apps-Script"的脚本时,似乎URL无法直接用于s3.putObject()
.并且,使用getDataAsString()
将输入的Blob转换为字符串类型.我认为这是您遇到问题的原因.
When I saw the script of "S3-for-Google-Apps-Script", it seems that the URL cannot be directly used for s3.putObject()
. And, the inputted blob is converted to the string type using getDataAsString()
. I think that this is the reason of your issue.
在这个答案中,我想建议修改GAS库"S3-for-Google-Apps-Script",以将字节数组用于payload
.
In this answer, I would like to propose to modify the GAS library of "S3-for-Google-Apps-Script" for using the byte array to payload
.
首先,请复制 S3-for-Google- Apps-Script ,请进行如下修改.
At first, please copy the GAS project of S3-for-Google-Apps-Script, and please modify this as follows.
关于S3.gs
文件中的S3.prototype.putObject
,请进行以下修改.
About S3.prototype.putObject
in the file of S3.gs
, please modify as follows.
request.setContent(object.getDataAsString());
到:
request.setContent(object.getBytes());
并且,关于S3Request.gs
文件中的S3Request.prototype.setContent
,请进行以下修改.
And, about S3Request.prototype.setContent
in the file of S3Request.gs
, please modify as follows.
if (typeof content != 'string') throw 'content must be passed as a string'
到:
// if (typeof content != 'string') throw 'content must be passed as a string'
并且,关于S3Request.gs
文件中的S3Request.prototype.getContentMd5_
,请进行以下修改.
And, about S3Request.prototype.getContentMd5_
in the file of S3Request.gs
, please modify as follows.
return Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, this.content, Utilities.Charset.UTF_8));
到:
return Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, this.content));
示例脚本:
对于上述修改后的脚本,请测试以下脚本.
Sample script:
And, for above modified script, please test the following script.
const imageUrl = "###"; // Please set the image URL.
const s3 = S3.getInstance(awsAccessKeyId, awsSecretKey); // Please set them.
const imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
s3.putObject(bucketName, 'test.png', imageBlob, { logRequests:true });
- 这样,您的令牌可以由修改后的库创建并使用.
- 当我检查这份正式文件时,我认为字节数组可能可以使用.
- By this, your token can be created by the modified library and use it.
- When I checked this official document, I thought that the byte array might be able to be used.
- PutObject
- S3-for-Google-Apps-Script
这篇关于如何将png文件上传到S3存储桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!