本地存储绑定规范

关于本地存储绑定组件的详细文档

配置

To set up the Local Storage binding, create a component of type bindings.localstorage. See this guide on how to create and apply a binding configuration.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. namespace: <NAMESPACE>
  6. spec:
  7. type: bindings.localstorage
  8. version: v1
  9. metadata:
  10. - name: rootPath
  11. value: <string>

元数据字段规范

字段必填绑定支持详情Example
rootPathYInput / OutputThe root path anchor to which files can be read / saved“/temp/files”

绑定支持

字段名为 ttlInSeconds

Create file

To perform a create file operation, invoke the Local Storage binding with a POST method and the following JSON body:

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

  1. {
  2. "operation": "create",
  3. "data": "YOUR_CONTENT"
  4. }

示例

Save text to a random generated UUID file

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

  1. curl -d "{ \"operation\": \"create\", \"data\": \"Hello World\" }" http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
  1. curl -d '{ "operation": "create", "data": "Hello World" }' \
  2. http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
Save text to a specific file
  1. curl -d "{ \"operation\": \"create\", \"data\": \"Hello World\", \"metadata\": { \"fileName\": \"my-test-file.txt\" } }" \
  2. http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
  1. curl -d '{ "operation": "create", "data": "Hello World", "metadata": { "fileName": "my-test-file.txt" } }' \
  2. http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
Save a binary file

To upload a file, encode it as Base64. The binding should automatically detect the Base64 encoding.

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

响应

响应体将包含以下JSON:

  1. {
  2. "fileName": "<filename>"
  3. }

Get file

To perform a get file operation, invoke the Local Storage binding with a POST method and the following JSON body:

  1. {
  2. "operation": "get",
  3. "metadata": {
  4. "fileName": "myfile"
  5. }
  6. }

Example

  1. curl -d '{ \"operation\": \"get\", \"metadata\": { \"fileName\": \"myfile\" }}' http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
  1. curl -d '{ "operation": "get", "metadata": { "fileName": "myfile" }}' \
  2. http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

响应

The response body contains the value stored in the file.

List files

To perform a list files operation, invoke the Local Storage binding with a POST method and the following JSON body:

  1. {
  2. "operation": "list"
  3. }

If you only want to list the files beneath a particular directory below the rootPath, specify the relative directory name as the fileName in the metadata.

  1. {
  2. "operation": "list",
  3. "metadata": {
  4. "fileName": "my/cool/directory"
  5. }
  6. }

Example

  1. curl -d '{ \"operation\": \"list\", \"metadata\": { \"fileName\": \"my/cool/directory\" }}' http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
  1. curl -d '{ "operation": "list", "metadata": { "fileName": "my/cool/directory" }}' \
  2. http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

响应

The response is a JSON array of file names.

Delete file

To perform a delete file operation, invoke the Local Storage binding with a POST method and the following JSON body:

  1. {
  2. "operation": "delete",
  3. "metadata": {
  4. "fileName": "myfile"
  5. }
  6. }

Example

  1. curl -d '{ \"operation\": \"delete\", \"metadata\": { \"fileName\": \"myfile\" }}' http://localhost:<dapr-port>/v1.0/bindings/<binding-name>
  1. curl -d '{ "operation": "delete", "metadata": { "fileName": "myfile" }}' \
  2. http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

响应

An HTTP 204 (No Content) and empty body will be returned if successful.

元数据信息

By default the Local Storage output binding auto generates a UUID as the file name. It is configurable in the metadata property of the message.

  1. {
  2. "data": "file content",
  3. "metadata": {
  4. "fileName": "filename.txt"
  5. },
  6. "operation": "create"
  7. }

相关链接