这是我在网上找到的解决方案:[ https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html]
function addPhoto(albumName) {
var files = document.getElementById('photoupload').files;
if (!files.length) {
return console.log('Please choose a file to upload first.');
}
var file = files[0];
var fileName = file.name;
var albumPhotosKey = encodeURIComponent(albumName) + '//';
var photoKey = albumPhotosKey + fileName;
s3.upload({
Key: photoKey,
Body: file,
ACL: 'public-read'
}, function(err, data) {
if (err) {
return console.log('There was an error uploading your photo: ', err.message);
}
console.log('Successfully uploaded photo.');
viewAlbum(albumName);
});
}
但是,在我当前的环境中,没有称为“文档”的概念。我真的不知道“文档”是如何工作的。我可以在当前环境中包含“文档”吗?或者我可以使用其他东西来获取本地文件[图像]?非常感谢!
s3.upload:https ://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
您应该指定您的环境是什么。document
对象仅在 HTML(浏览器中运行的网页)中有意义。如果您不是在浏览器中运行而是独立运行,那么您可能会使用 Node.js。
正如文档所说,Body
参数可以是 Buffer、Typed Array、Blob、String 或 ReadableStream。
因此,Node.js 中本地文件的简单上传可能如下所示:
var fs = require('fs');
var stream = fs.createReadStream('/my/file');
s3.upload({
Bucket: 'mybucket',
Key: 'myfile'
Body: stream
}, function(err, data) {
if (err) return console.log('Error by uploading.', err.message);
console.log('Successfully uploaded.');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句