码迷,mamicode.com
首页 > 编程语言 > 详细

AWS S3 JAVASCRIPT SDK 上传

时间:2020-12-19 12:21:45      阅读:2      评论:0      收藏:0      [点我收藏+]

标签:creat   script   upload   rgb   listen   file   click   rip   形式   

<!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.806.0.min.js"></script> -->
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.235.1.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">
    /// <reference types="aws-sdk" />
    var credentials = {
        accessKeyId: xxx,
        secretAccessKey: xxxxxxxxxxxxxxx
    }; //秘钥形式的登录上传
    AWS.config.update(credentials);
    AWS.config.region = us-east-1; //设置区域
    AWS.config.endpoint = http://114.xx.xx.xx:9000/ysx;
    AWS.config.sslEnabled = false;
    AWS.config.s3BucketEndpoint = true;
    
    // create bucket instance
    var bucket = new AWS.S3({ params: { Bucket: xxx } }); //选择桶
    var fileChooser = document.getElementById(file-chooser);
    var button = document.getElementById(upload-button);
    var results = document.getElementById(results);
    button.addEventListener(click, function() {
        var file = fileChooser.files[0];
        if (file) {
            results.innerHTML = ‘‘;
            var params = { Key: file.name, ContentType: file.type, Body: file, Access-Control-Allow-Credentials: *, ACL: public-read }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
            console.log(params)
            bucket.upload(params, function(err, data) {
                console.log(err); //打印出错误
                results.innerHTML = err ? ERROR! : UPLOADED.;
                console.log(data);
            });
        } else {
            results.innerHTML = Nothing to upload.;
        }
    }, false);
    </script>
</body>

</html>

 

AWS S3 JAVASCRIPT SDK 上传

标签:creat   script   upload   rgb   listen   file   click   rip   形式   

原文地址:https://www.cnblogs.com/goldenstones/p/14134357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!