amazon-web-services - 上传到amazon aws s3时如何获取本地文件?
问题描述
这是我在网上找到的解决方案:[ 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 中有意义,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.');
});
推荐阅读
- sql-server - 使用位域和全文搜索查询非常慢
- javascript - 如何为在 JS 的主开发中的任何子 div 中拖放的任何项目触发放置事件
- css - 如何避免 dompdf 中的分页符
- react-navigation - React Navigation - DrawerNavigator 内的 TopTabNavigator
- angular - 如何正确转义 ngstyle 背景图像
- c# - 将月份放在 MajorGrid 上,将天数放在 MinorGrid 上(范围条形图)
- node.js - 删除 Node JS 中的“Buffer() depreciation”警告
- ios - 为什么单击按钮时隐藏一个标题部分
- excel - 保存到 %temp% 文件夹
- android - Androidx Material TextInput 颜色不会改变