javascript 上传文件到 aws s3存储桶
<p>直接上代码</p><div class="cnblogs_code">
<pre><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- <script src="https://sdk.amazonaws.com/js/aws-sdk-2.410.0.min.js"></script> -->
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.235.1.min.js"></script>
</head>
<body>
<input id="file-chooser" type="file"/>
<button id="upload-button">Upload</button>
<p id="results"><p></p>
<script type="text/javascript">
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">/ <reference types="aws-sdk" /></span>
<span style="color: rgba(0, 0, 255, 1)">var</span> credentials =<span style="color: rgba(0, 0, 0, 1)"> {
accessKeyId: </span>'xxxxxxxxxxxxx'<span style="color: rgba(0, 0, 0, 1)">,
secretAccessKey: </span>'xxxxxxxxxxxxxx'<span style="color: rgba(0, 0, 0, 1)">
}; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">秘钥形式的登录上传</span>
<span style="color: rgba(0, 0, 0, 1)"> AWS.config.update(credentials);
AWS.config.region </span>= 'xxxxxxxxxxxx'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置区域</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> create bucket instance</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> bucket = <span style="color: rgba(0, 0, 255, 1)">new</span> AWS.S3({ params: { Bucket: 'xxxxxxxxx' } }); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">选择桶</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> fileChooser = document.getElementById('file-chooser'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> button = document.getElementById('upload-button'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> results = document.getElementById('results'<span style="color: rgba(0, 0, 0, 1)">);
button.addEventListener(</span>'click', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> file = fileChooser.files;
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (file) {
results.innerHTML </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> params = { Key: file.name, ContentType: file.type, Body: file, 'Access-Control-Allow-Credentials': '*', 'ACL': 'public-read' }; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">key可以设置为桶的相抵路径,Body为文件, ACL最好要设置</span>
<span style="color: rgba(0, 0, 0, 1)"> console.log(params)
bucket.upload(params, </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(err, data) {
console.log(err); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印出错误</span>
results.innerHTML = err ? 'ERROR!' : 'UPLOADED.'<span style="color: rgba(0, 0, 0, 1)">;
});
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
results.innerHTML </span>= 'Nothing to upload.'<span style="color: rgba(0, 0, 0, 1)">;
}
}, </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
</span></script>
</body>
</html></pre>
</div>
<p>2.需要在aws s3中的cors进行配置如下图</p>
<p><img src="https://img2020.cnblogs.com/blog/2043788/202005/2043788-20200523155037569-748778265.png"></p>
<p> 附代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="UTF-8"</span><span style="color: rgba(0, 0, 255, 1)">?></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">CORSConfiguration </span><span style="color: rgba(255, 0, 0, 1)">xmlns</span><span style="color: rgba(0, 0, 255, 1)">="http://s3.amazonaws.com/doc/2006-03-01/"</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">CORSRule</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">AllowedOrigin</span><span style="color: rgba(0, 0, 255, 1)">></span>*<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">AllowedOrigin</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>GET<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>PUT<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>POST<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>DELETE<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">AllowedMethod</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">ExposeHeader</span><span style="color: rgba(0, 0, 255, 1)">></span>ETag<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">ExposeHeader</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">AllowedHeader</span><span style="color: rgba(0, 0, 255, 1)">></span>*<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">AllowedHeader</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">CORSRule</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">CORSConfiguration</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p> </p>
<p>如果出现 TypeError: m.upload.addEventListener is not a function</p>
<p>查看是否在文件中引入mock把它去掉就ok了</p><br><br>
来源:https://www.cnblogs.com/lyjfight/p/12942829.html
頁:
[1]