0
我是新来的aws。 我想做一个web应用程序,它会让用户在文本字段中插入文本(假设有两个文本字段),然后将插入的文本转换为Json文件并保存到S3存储桶。我使用的是javascript和Angularjs。任何人都可以帮我完成这个过程吗?有没有办法将数组转换成json文件并将其保存/发布到S3存储桶(aws)中?
我是新来的aws。 我想做一个web应用程序,它会让用户在文本字段中插入文本(假设有两个文本字段),然后将插入的文本转换为Json文件并保存到S3存储桶。我使用的是javascript和Angularjs。任何人都可以帮我完成这个过程吗?有没有办法将数组转换成json文件并将其保存/发布到S3存储桶(aws)中?
这是一个相当基本的流程来创建。它需要一些调整,因为您需要创建触发器来执行代码,如按钮单击或您有什么,但它应该足够接近您要查找的内容。
这一过程将简洁如下:
的HTML
这里的元素会被你的JavaScript/AngularJS代码访问。
<input type="text" id="your_id_1" value="example1" />
<input type="text" id="your_id_2" value="example2" />
的JavaScript
此代码将从你输入提取值,并创建一个JSON对象转移。
// This script tag will download the AWS SDK and make it available to your code
<script src="https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.6.15/aws-sdk.min.js"></script>
// This script tag will contain your own code
<script>
// Get the values from the inputs into variables
var input_1_value = document.getElementById("your_id_1").value; //example1
var input_2_value = document.getElementById("your_id_2").value; //example2
// Create the JSON object
var json_object = {
"input_1_value" : input_1_value,
"input_2_value" : input_2_value
};
// Create an instance of the S3 client
var s3 = new AWS.S3();
AWS.config.update({ accessKeyId: "your_access_key", secretAccessKey: "your_secret_key" });
AWS.config.region = 'us-east-1';
// Provide the necessary details for the file upload (The bucket to upload it to and the file name you want it to have.)
var params = {
Bucket: 'your_bucket_name', /* required */
Key: 'your_file_name' /* required */
};
// Perform the upload, which will print either outcome (result or error) in the console.
s3.putObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
</script>
桶政策
上面的代码将无法正常工作,直到您应用CORS政策要上传的,让你的JavaScript代码来与它进行交互桶。 我不会详细解释CORS,你会在google上找到大量的文档。我们不得不使用这个的原因解释为here。
要应用桶政策:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>https://example.org</AllowedOrigin>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
<ExposeHeader>ETag</ExposeHeader>
<ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
</CORSRule>
</CORSConfiguration>
欢迎粘贴成箱的StackOverflow!您能否澄清一下您的问题 - 您是否在寻求帮助将文件复制到Amazon S3的部分,或者您是否正在寻求JSON部分的帮助?随时更新您的问题,以显示您到目前为止尝试过的代码以及您遇到的任何错误/问题。这样,你将更有可能收到你的问题的答案。 –