前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从前端直接往aws上传文件总结

从前端直接往aws上传文件总结

作者头像
零式的天空
发布2022-03-22 12:54:44
1K0
发布2022-03-22 12:54:44
举报
文章被收录于专栏:零域Blog

AWS S3 javascript上传文件 1 To upload the file successfully, you need to enable CORS configuration on S3.

Generally, it is not advisable to display your keys directly on page, so you can use Amazon Cognito or web identity federation feature.

To know more use the below given link : http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html

2 Set Aws Bunket CORS like this:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
 <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
	<CORSRule><AllowedOrigin>*</AllowedOrigin>
	<AllowedMethod>GET</AllowedMethod>
	<AllowedMethod>PUT</AllowedMethod>
	<AllowedMethod>POST</AllowedMethod>
	<AllowedMethod>DELETE</AllowedMethod>
	<MaxAgeSeconds>3000</MaxAgeSeconds>
	<ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
	<ExposeHeader>x-amz-request-id</ExposeHeader>
	<ExposeHeader>x-amz-id-2</ExposeHeader>
	<ExposeHeader>ETag</ExposeHeader>
	<AllowedHeader>*</AllowedHeader>
	</CORSRule></CORSConfiguration>

3 Set the Aws Config in jsp like this:

代码语言:javascript
复制
AWS.config.update({
	region: 'cn-north-1'
});
AWS.config.apiVersions = {
	s3: '2006-03-01'
}; 
	var accessKeyId = "xx";
	var secretAccessKey = xx";
	var token = "xx";//如果不是生成的临时token,可以不要
	var creds = new AWS.Credentials({
		accessKeyId: accessKeyId,
		secretAccessKey: secretAccessKey,
		sessionToken: token //如果不是生成的临时token,可以不要
	});
	var bucketName = "xx";
	var s3Param = {
		params: {
			Bucket: bucketName,
			CORSConfiguration:{
				CORSRules:[
					{
						AllowedHeaders:["Authorization"],
						AllowedMethods:["GET","POST","PUT"],
						AllowedOrigins:["http://*","https://*"],
						ExposeHeaders:[""],
						MaxAgeSeconds:0
					}
				]
			}
		},
		credentials : creds
	}
 bucket = new AWS.S3(s3Param);

4 Set input form like this :

代码语言:javascript
复制
	<span class="btn" >选择文件</span>
<input type="text" value="未选择文件" id="filetext" class="filetext file-text" disabled="disabled">
<input type="file" name="myfiles" id="myBlogImageOfBasicmodeloffline"  data-type=".zip"  data-message="非zip文件" class="filehide file-select"  onchange="selectBasicmodel(this)" style="cursor:pointer;left: 100px;" multiple>
<input type="button" id="fileUploadButton" onclick="offlineFileUpload()" value="上传"  class="btn">

5 clike the upload button like this :

代码语言:javascript
复制
function offlineFileUpload(){
	if($.isBlank($('#myBlogImageOfBasicmodeloffline').val())){
		return;
	} 
	var fileChooser = document.getElementById('myBlogImageOfBasicmodeloffline');
	 	for(var f = 0 ;f<fileChooser.files.length;f++){
			var file = fileChooser.files[f];
			if (file){ 
				var params = {
					Key: file.name, //如果有路径,可以 a/b/a.txt
					ContentType: file.type,
					Body: file};
				var options = {partSize: 10 * 1024 * 1024, queueSize: 1};
				var request = bucket.upload(params,options);
			        request.on('httpUploadProgress', function(evt) {
					console.log("Uploaded :: " + parseInt((evt.loaded * 100) / evt.total)+'%');
				}).send(function(err, data) { 
				  //每次上传成功一个文件,可以TODO 
				});
			}else {
				console.log('Nothing to upload.');
			}
		}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档