Upload a file with $.ajax to AWS S3 with a pre-signed url
转载自:https://gist.github.com/guumaster/9f18204aca2bd6c71a24
生成预签名的Demo文档:https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/PresignedUrlUploadObjectJavaSDK.html
前端写法:
生成签名:(.js)
var s3 = new AWS.S3({
accessKeyId: '<YOUR_ACCESS_KEY>',
secretAccessKey: '<YOUR_SECRET_ACCESS_KEY>'
}); var uploadPreSignedUrl = s3.getSignedUrl('putObject', {
Bucket: '<THE_BUCKET_NAME>',
Key: '<THE_UPLOADED_FILENAME>',
ACL: 'authenticated-read',
// This must match with your ajax contentType parameter
ContentType: 'binary/octet-stream' /* then add all the rest of your parameters to AWS puttObect here */
}); var downloadPreSignedUrl = s3.getSignedUrl('getObject', {
Bucket: '<THE_BUCKET_NAME>',
Key: '<THE_UPLOADED_FILENAME>',
/* set a fixed type, or calculate your mime type from the file extension */
ResponseContentType: 'image/jpeg'
/* and all the rest of your parameters to AWS getObect here */
}); // now you have both urls
console.log( uploadPreSignedUrl, downloadPreSignedUrl );
前端页面:(.html)
<form id="theForm" method="POST" enctype="multipart/form-data" >
<input id="theFile" name="file" type="file"/>
<button id="theButton" type="submit">send 1</button>
</form> After you uploaded the file you can <a href="<YOUR_PRE_SIGNED_DOWNLOAD_URL_HERE>">download it here</a>
<script src="upload.js"></script>
upload:(.js)
// Remember to include jQuery somewhere. $(function() { $('#theForm').on('submit', sendFile);
}); function sendFile(e) {
e.preventDefault(); // get the reference to the actual file in the input
var theFormFile = $('#theFile').get()[0].files[0]; $.ajax({
type: 'PUT',
url: "<YOUR_PRE_SIGNED_UPLOAD_URL_HERE>",
// Content type must much with the parameter you signed your URL with
contentType: 'binary/octet-stream',
// this flag is important, if not set, it will try to send data as a form
processData: false,
// the actual file is sent raw
data: theFormFile
})
.success(function() {
alert('File uploaded');
})
.error(function() {
alert('File NOT uploaded');
console.log( arguments);
}); return false;
});
}
Upload a file with $.ajax to AWS S3 with a pre-signed url的更多相关文章
- Uploading File using Ajax and receiving binary data in Asp.net (C#)[转]
基础知识,可由此衍生.原文:http://uniapple.net/blog/?p=2050 In this post, I will show you how to upload a file us ...
- Edit Static Web File Http Header Metadata of AWS S3 from SDK | SDK编程方式编辑储存在AWS S3中Web类文件的Http Header元数据
1.Motivation | 起因 A requirement from the product department requires download image from AWS S3 buck ...
- 用Node完成AWS S3的Upload流程之全世界最简版
开场: 查了两天文档,Error了38次,最后索性去掉所有附加条件, 连界面也不要了,在命令行里跑通了一坨最干瘪的Upload流程! 还冒着热气…… 在此先做记录,明天可以搭配美美的界面继续调试了. ...
- aws s3文件上传设置accesskey、secretkey、sessiontoken
背景: 最近跟进的项目会封装aws S3资源管理细节,对外提供获取文件上传凭证的API,业务方使用获取到的凭证信息直接请求aws进行文件上传.因此,测试过程需要验证S3文件上传的有效性.aws官网有提 ...
- Amazon AWS S3 操作手册
Install the SDK The recommended way to use the AWS SDK for Java in your project is to consume it fro ...
- Node开发文件上传系统及向七牛云存储和亚马逊AWS S3的文件上传
背景起,有奏乐: 有伟人曰:学习技能的最好途径莫过于理论与实践相结合. 初学Node这货时,每每读教程必会Fall asleep. 当真要开发系统时,顿觉精神百倍,即便踩坑无数也不失斗志. 因为同团队 ...
- java操作AWS S3一些坑记录
1,aws sdk jar版本不一致问题 一开始我在pom.xml中只配置了如下aws-java-sdk-s3 <!-- https://mvnrepository.com/artifact/c ...
- AWS S3 对象存储服务
虽然亚马逊云非常牛逼,虽然亚马逊云财大气粗,虽然亚马逊用的人也非常多,可是这个文档我简直无法接受,特别是客服,令人发指的回复速度,瞬间让人无语,可是毕竟牛逼.忍了,躺一次坑而已 1.图片上传 1.1 ...
- AWS s3 python sdk code examples
Yet another easy-to-understand, easy-to-use aws s3 python sdk code examples. github地址:https://github ...
随机推荐
- Java 读取 Json格式的 内容
一.Json 报文格式如下: 二.获取 Json 报文中字段的内容 import java.io.IOException; import com.fasterxml.jackson.core.Json ...
- 传统方式和插件方式 分别实现 分页 功能 pageHelper 插件
实现分页 这里提供两种方式 一种是传统的分页方式 一种是基于pageHelper插件 实现的分类 推荐使用后者 前者是一般开发的方式 思路 先手动创建一个 pageUtil 工具 ...
- git-format-patch
使用方法: git diff ${old-commit} ${new-commit} > commit-operation.patch OR git format- b1af44f > c ...
- OSS内文件如何设置为无时间限制的下载链接
OSS内文件如何设置为无时间限制的下载链接 想把一些文件上传到OSS里,把OSS当网盘用,做成分享的下载链接 发现获取的链接都是有时间限制的 有没有取消这个时间限制的功能或者方法 请将object的权 ...
- atom编辑器安装插件报错。。
Checking for native build tools failed gyp info it worked if it ends with ok gyp info using node-gyp ...
- c#拷贝整个文件夹到指定文件夹下(非递归)
public static void CopyEntireDir(string sourcePath, string destPath) { //Now Create all of the direc ...
- 转:浅谈SimpleDateFormat的线程安全问题
转自:https://blog.csdn.net/weixin_38810239/article/details/79941964 在实际项目中,我们经常需要将日期在String和Date之间做转化, ...
- [ipsec][crypto] IKEv2的协商交互分析
一: 无论协商了什么样的加密算法.DH都交换一块长度为32byte的内存,作为key. IKE和esp的key,分别基于这块内存生成. 二: 当esp的算法协商没有指定dh group时,rekey将 ...
- 如何让模拟的json数据接口能够正常的在手机上有效果
1. 确保手机与PC在同一个ip网下 这里我是通过------------360随身WIFI,20块钱淘宝上卖的,外观像U盘一样的,直接插在电脑的USB上就能在PC上创建一个WiFi,手机连接上就可以 ...
- spark-sql缩减版样例:获取每日top3搜索词和各自的次数,包括总次数
//获取出每天前3的搜索词 ArrayList<String> log = new ArrayList<String>(); log.add("2015-10-01, ...