php - 如何将文件从我的 Web UI 直接上传到 Amazon S3?
问题描述
我有一个 PHP 代码可以将文件从本地系统上传到 Amazon S3。我还有 PHP 网页,可以让用户上传文件并提交。我的要求是我需要将用户上传的文件直接上传到 Amazon S3。有没有办法将文件直接从 UI 上传到 Amazon S3 而无需将其保存在系统中。?如果有办法我该怎么做。
提前致谢。
解决方案
是的,您可以在不使用sdk-for-javascript之间保留任何系统的情况下做到这一点
您可以参考执行此操作的示例网页。
以下是该页面的代码片段
function addPhoto(albumName) {
var files = document.getElementById('photoupload').files;
if (!files.length) {
return alert('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 alert('There was an error uploading your photo: ', err.message);
}
alert('Successfully uploaded photo.');
viewAlbum(albumName);
});
}
要从浏览器脚本将您的凭据设置为 SDK,以下是按推荐顺序排列的方法:
- 使用 Amazon Cognito Identity 对用户进行身份验证并提供凭证
- 使用 Web 联合身份
- 在脚本中硬编码
推荐阅读
- angularjs - RESTful API Set-Cookie 标头未与请求标头一起传递到后端
- lua - 我在roblox studio中制作了关卡系统,脚本似乎还可以,但是在xp达到最大值后没有升级,而是继续进行,我该怎么办?
- shopify - Shopify 应用程序:任何检测主题更改或使主题部分即使在主题更改后仍然存在的事件?
- python - 如何在 Tkinter 应用程序中集成命令行界面窗口
- python-multiprocessing - 在python多进程下重新启动子进程
- javascript - React Native 和 Graphql 实时聊天应用程序无法在 iOS Expo Go 上运行
- mongodb - Mongo Atlas 搜索索引数字字段精确匹配搜索
- node.js - Supertest:在 Jest 环境被拆除后,您正在尝试“导入”一个文件
- katalon-studio - 从全局变量中检索值的计数并从值中选择任意随机数
- python - 如何更改列表列表中的值?