@aws-sdk/s3- 指定员额
此软件包提供了一个生成 URL 和字段的函数 。 没有 AWS 证书的用户可以使用 URL 和字段 向 S3 提出 POST 请求 。 服务器侧面特性的文档可见于 S3 API 参考文献。请阅读相关章节以了解更多上下文.
导入
Java 脚本示例 :
const { createPresignedPost } = require("@aws-sdk/s3-presigned-post");
const { S3Client } = require("@aws-sdk/client-s3");
ES6 示例
import { createPresignedPost } from "@aws-sdk/s3-presigned-post";
import { S3Client } from "@aws-sdk/client-s3";
创建 POST 策略
您可选择在预签职位上附加政策。 它规定了请求必须满足的条件清单. 例如:
const Conditions = [
{ acl: "bucket-owner-full-control" },
{ bucket: "johnsmith" },
["starts-with", "$key", "user/eric/"],
];
访问 S3 POST 文档
支持的政策要素。 如果包含条件,则必须在 Fields 参数
还有 字段词典不会根据条件自动添加一个值 .
创建指定员额
用户可以为 POST 请求生成所需的url 和字段 :
const client = new S3Client({ region: "us-west-2" });
const Bucket = "johnsmith";
const Key = "user/eric/1";
const Fields = {
acl: "bucket-owner-full-control",
};
const { url, fields } = await createPresignedPost(client, {
Bucket,
Key,
Conditions,
Fields,
Expires: 600, //Seconds before the presigned post expires. 3600 by default.
});
这个 Bucket, (中文(简体) ), Key 中的其他数值 Fields 必须符合下列条件: Conditions编辑 Key 还可以
包含 ${filename} 将自动被所提供文件的名称所取代。 见 s3 参考文献
更多信息.
使用 HTML 窗体张贴文件
您也可以用 HTML 格式发布文件 :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<!-- Copy the 'url' value returned by createPresignedPost() -->
<form action="URL_VALUE" method="post" enctype="multipart/form-data">
<!-- Copy the 'fields' key:values returned by S3Client.generate_presigned_post() -->
<input type="hidden" name="key" value="VALUE" />
<input type="hidden" name="AWSAccessKeyId" value="VALUE" />
<input type="hidden" name="policy" value="VALUE" />
<input type="hidden" name="signature" value="VALUE" />
File:
<input type="file" name="file" /> <br />
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>
</body>
</html>
在 Node.js 中使用 FormData 的邮递文件
在节点.js中,使用 form-data 发布文件的软件包 :
const { createReadStream } = require("fs");
const FormData = require("form-data");
const form = new FormData();
Object.entries(fields).forEach(([field, value]) => {
form.append(field, value);
});
form.append("file", createReadStream("path/to/a/file"));
form.submit(url, (err, res) => {
//handle the response
});