The documentation you are viewing is for Dapr v1.6 which is an older version of Dapr. For up-to-date documentation, see the latest version.

AWS S3 绑定规范

有关 AWS S3 绑定组件的详细文档

配置

要设置 AWS S3 绑定,请创建一个类型为 bindings.aws.s3 的组件。 请参阅本指南,了解如何创建和应用绑定配置。

有关身份验证相关属性的信息,请参阅 向 AWS 进行身份验证

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: <NAME>
  namespace: <NAMESPACE>
spec:
  type: bindings.aws.s3
  version: v1
  metadata:
  - name: bucket
    value: mybucket
  - name: region
    value: us-west-2
  - name: endpoint
    value: s3-us-west-2.amazonaws.com
  - name: accessKey
    value: *****************
  - name: secretKey
    value: *****************
  - name: sessionToken
    value: mysession
  - name: decodeBase64
    value: <bool>
  - name: encodeBase64
    value: <bool>
  - name: forcePathStyle
    value: <bool>

元数据字段规范

字段 必填 绑定支持 详情 示例
bucket 输出 要写入的 S3 存储桶的名称 "bucket"
region 输出 指定的 AWS 区域(region) "us-east-1"
endpoint 输出 指定的 AWS endpoint "s3-us-east-1.amazonaws.com"
accessKey 输出 要访问此资源的 AWS 访问密钥 "key"
secretKey 输出 要访问此资源的 AWS 密钥访问 Key "secretAccessKey"
sessionToken 输出 要使用的 AWS 会话令牌 "sessionToken"
forcePathStyle 输出 目前,Amazon S3 开发工具包支持虚拟主机样式和路径样式访问。 true 是路径样式的格式,如 https://<endpoint>/<your bucket>/<key>false 是托管样式的格式,如 https://<your bucket>.<endpoint>/<key>。 默认值为 false true, false
decodeBase64 输出 在保存到存储桶之前解码 base64 文件内容的配置。 (保存有二进制内容的文件时)。 true 是唯一允许的正值。 其他正值,如 "True","1"<code> 是不允许的。 默认值为 <code>false true, false
encodeBase64 输出 在返回内容之前对 base64 文件内容进行编码的配置。 (在打开带有二进制内容的文件时有用)。 true 是唯一允许的正值。 其他正值,如 "True","1"<code> 是不允许的。 默认值为 <code>false true, false

绑定支持

该组件支持如下操作的 输出绑定

创建文件

要执行一个创建操作,需要调用 AWS S3 绑定一个 <0>POST</0> 方法和下面的 JSON:

注意:默认情况下,会随机生成一个UUID。 参见下面所示的支持的元数据设置名称

{
  "operation": "create",
  "data": "YOUR_CONTENT"
}

示例

把文本保存到一个随机生成的 UUID 文件

在Windows上,使用cmd提示符(PowerShell有不同的转义机制)。

curl -d "{ \"operation\": \"create\", \"data\": \"Hello World\" }" http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "create", "data": "Hello World" }' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
将文本保存到指定文件

curl -d "{ \"operation\": \"create\", \"data\": \"Hello World\", \"metadata\": { \"key\": \"my-test-file.txt\" } }" \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "create", "data": "Hello World", "metadata": { "key": "my-test-file.txt" } }' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
将文件保存到对象

要上传一个文件,将其编码为Base64,并让绑定知道要对它进行反序列化:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: <NAME>
  namespace: <NAMESPACE>
spec:
  type: bindings.aws.s3
  version: v1
  metadata:
  - name: bucket
    value: mybucket
  - name: region
    value: us-west-2
  - name: endpoint
    value: s3-us-west-2.amazonaws.com
  - name: accessKey
    value: *****************
  - name: secretKey
    value: *****************
  - name: sessionToken
    value: mysession
  - name: decodeBase64
    value: <bool>
  - name: forcePathStyle
    value: <bool>

然后你就可以像平常一样上传了:


curl -d "{ \"operation\": \"create\", \"data\": \"YOUR_BASE_64_CONTENT\", \"metadata\": { \"key\": \"my-test-file.jpg\" } }" http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "create", "data": "YOUR_BASE_64_CONTENT", "metadata": { "key": "my-test-file.jpg" } }' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

响应

响应体将包含以下JSON:

{
    "location":"https://<your bucket>.s3.<your region>.amazonaws.com/<key>",
    "versionID":"<version ID if Bucket Versioning is enabled"
}

获取对象

要执行获取文件操作,请使用 POST 方法和以下 JSON 调用 AWS S3 绑定:

{
  "operation": "get",
  "metadata": {
    "key": "my-test-file.txt"
  }
}

元数据参数包括:

  • key - 对象的名称

示例


curl -d '{ \"operation\": \"get\", \"metadata\": { \"key\": \"my-test-file.txt\" }}' http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "get", "metadata": { "key": "my-test-file.txt" }}' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

响应

响应正文包含存储在对象中的值。

删除对象

要执行删除文件操作,请使用 POST 方法和以下 JSON 调用 AWS S3 绑定:

{
  "operation": "delete",
  "metadata": {
    "key": "my-test-file.txt"
  }
}

元数据参数包括:

  • key - 对象的名称

示例

删除对象

curl -d '{ \"operation\": \"delete\", \"metadata\": { \"key\": \"my-test-file.txt\" }}' http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "delete", "metadata": { "key": "my-test-file.txt" }}' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

响应

如果成功,将返回 HTTP 204(没有内容)和空报文体。

列出对象

要执行列出对象操作,请使用 POST 和以下 JSON 调用 S3 绑定:

{
  "operation": "list",
  "data": {
    "maxResults": 10,
    "prefix": "file",
    "marker": "hvlcCQFSOD5TD",
    "delimiter": "i0FvxAn2EOEL6"
  }
}

参数的含义是:

  • maxResults - (可选)设置响应中返回的最大键数。 默认情况下,该操作最多返回 1000 个键名。 响应可能包含较少的键,但永远不会包含更多键
  • prefix - (可选)只返回以指定前缀开头的键。
  • marker - (可选)标记是您希望 Amazon S3 从何处开始返回。 Amazon S3 返回此指定键之后的数据。 标记可以是存储桶中的任何键。 然后,可以在后续调用中使用标记值来请求下一组数据。
  • delimiter - (可选)分隔符是用于对键进行分组的字符。

响应

响应正文包含找到的对象列表。

对象列表将以以下格式的 JSON 数组返回:

{
    "CommonPrefixes": null,
    "Contents": [
        {
            "ETag": "\"7e94cc9b0f5226557b05a7c2565dd09f\"",
            "Key": "hpNdFUxruNuwm",
            "LastModified": "2021-08-16T06:44:14Z",
            "Owner": {
                "DisplayName": "owner name",
                "ID": "owner id"
            },
            "Size": 6916,
            "StorageClass": "STANDARD"
        }
    ],
    "Delimiter": "",
    "EncodingType": null,
    "IsTruncated": true,
    "Marker": "hvlcCQFSOD5TD",
    "MaxKeys": 1,
    "Name": "mybucketdapr",
    "NextMarker": "hzaUPWjmvyi9W",
    "Prefix": ""
}

相关链接