在浏览器中运行代码时未创建S3存储桶?

时间:2019-06-17 19:32:56

标签: javascript amazon-web-services amazon-s3 browserify

当我在控制台上运行以下代码(如myFunc.myfunc(“ SomeText”))时,它会创建存储桶,但是当我在按钮上使用相同的功能时,它不会创建任何东西。有时确实会创建,但是您必须等待大约20至30分钟,才能下次正确处理。

我在客户端使用node.js,通过browserify使require()在CORS策略的客户端和任何位置运行。

JS文件-

//加载SDK和UUID

function uploadToS3(dataForBody) {

    var AWS = require('aws-sdk');   //Browserify will be used and bundled in uploadDocsBundle
    var uuid = require('uuid');



    AWS.config.accessKeyId = 'my access key id';
    AWS.config.secretAccessKey = 'my secret access id';
    AWS.config.region = 'eu-west-1';


    // Create an S3 client
    var s3 = new AWS.S3();

    // Create a bucket and upload something into it
    var bucketName = 'node-sdk-sample-' + uuid.v4();
    var keyName = 'hello_world.txt';
    s3.createBucket({ Bucket: bucketName }, function () {

        var params = {
            Bucket: bucketName, Key: keyName, Body: dataForBody
        };
        s3.putObject(params, function (err, data) {
            if (err) 
                console.log(err);
            else
                console.log("Successfully uploaded data to " + bucketName + "/" + keyName);
        });
    });

}

(function () {
    var cors_api_host = 'cors-anywhere.herokuapp.com';
    var cors_api_url = 'https://' + cors_api_host + '/';
    var slice = [].slice;
    var origin = window.location.protocol + '//' + window.location.host;
    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function () {
        var args = slice.call(arguments);
        var targetOrigin = /^https?:\/\/([^\/]+)/i.exec(args[1]);
        if (targetOrigin && targetOrigin[0].toLowerCase() !== origin &&
            targetOrigin[1] !== cors_api_host) {
            args[1] = cors_api_url + args[1];
        }
        return open.apply(this, args);
    };
})();



======================================================

HTML File
 <button type="submit" onClick="myFunc.myfunc('hello')" class="btn btn-primary">Submit</button>

Edit2:-
=======

// Load the SDK and UUID
module.exports = { myfunc: uploadToS3 };
function uploadToS3(dataForBody) {

    var AWS = require('aws-sdk');   //Browserify will be used and bundled in uploadDocsBundle
    var uuid = require('uuid');





    AWS.config.accessKeyId = 'AccessKey';
    AWS.config.secretAccessKey = 'SecretAccessID';
    AWS.config.region = 'eu-west-1';




    // Create a bucket and upload something into it
    var bucketName = 'node-sdk-sample-' + uuid.v4();
    var keyName = 'hello_world2.txt';
    var bucketPromise = new AWS.S3().createBucket({ Bucket: bucketName }).promise();
 //Handle promise fulfilled/rejected states
   bucketPromise.then(
     function (data) {
            // Create params for putObject call
          var objectParams = { Bucket: bucketName, Key: keyName, Body: 'Hello World!' };

         // Create object upload promise

      var uploadPromise = new AWS.S3().putObject(objectParams).promise();
    uploadPromise.then(
      function(data) {
        console.log("Successfully uploaded data to " + bucketName + "/" + keyName);
      });

     }).catch(
          function (err) {
            console.error(err, err.stack);
       });


}
(function () {
    var cors_api_host = 'cors-anywhere.herokuapp.com';
    var cors_api_url = 'https://' + cors_api_host + '/';
    var slice = [].slice;
    var origin = window.location.protocol + '//' + window.location.host;
    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function () {
        var args = slice.call(arguments);
        var targetOrigin = /^https?:\/\/([^\/]+)/i.exec(args[1]);
        if (targetOrigin && targetOrigin[0].toLowerCase() !== origin &&
            targetOrigin[1] !== cors_api_host) {
            args[1] = cors_api_url + args[1];
        }
        return open.apply(this, args);
    };
})();   

0 个答案:

没有答案