put
The put
method is used to upload data to the object store service.
async function put(
key: string,
value: Blob | ArrayBufferLike | ArrayBufferView | ReadableStream | JSONObject | string
): Promise<StorageItem>;
Reference
Overview
You upload data to the object store service by calling the put
method.
import { myStorage } from "#elements";
...
const myStringData = "xxx";
await myStorage.put("my-data/string.txt", myStringData);
...
Parameters
key
: A string that uniquely identifies the object. This string should start and end with letters or numbers, and the middle can contain letters, numbers, dots, slashes, hyphens, or underscores.value
: Multiple data types are supported for data to be uploaded.
Returns
The put
method does not return a value
Caveats
- Each Storage Element is a separate storage space that doesn't talk to each other. In the same Storage Element, if the same Key is uploaded repeatedly, the value of that Key will be overwritten by the new value.
- If you are uploading more than 1MB of data, we recommend streaming it, such as Readable or ReadableStream, to avoid OOM crashes.
- Storage Element storage service has upload speed limit. If you upload large files, please increase the timeout time or use asynchronous method to upload to avoid errors caused by long interface response time.
- Storage Element The storage service has a single file size limit of 1024MB (based on usage). Files that exceed this size will be rejected.
- When you delete a Babel app, its Storage is cleared. Please operate with caution.
- Babel recognizes the extension of the item path as the default ContentType, and if it doesn't, it will set it to "binary/octet-stream". You can also upload a Blob object to specify a ContentType.
Examples
Upload a Buffer object
Here is an example of using the `Storage.put`` method in the HTTP Element to upload a Buffer object:
import * as Koa from "koa";
import { myStorage } from "#elements";
export default async function (request: Koa.Request, response: Koa.Response, ctx: Koa.Context) {
const myBufferData = Buffer.from("xxx");
await myStorage.put("my-data/buffer.txt", myBufferData);
// The contentType is automatically recognized as "text/plain"
return "success";
}
Upload a Blob object
Here is an example of using the Storage.put
method in the HTTP Element to upload a Blob object:
import * as Koa from "koa";
import { myStorage } from "#elements";
export default async function (request: Koa.Request, response: Koa.Response, ctx: Koa.Context) {
const myBlobData = new Blob(["xxx"], { type: "text/javascript" })
await myStorage.put("my-data/blob.js", myBlobData);
// Blob.type is recognized, the contentType is automatically set to "text/javascript"
return "success";
}
Upload a Readable object
Here is an example of using the Storage.put
method in the HTTP Element to upload a Readable object:
import * as Koa from "koa";
import { myStorage } from "#elements";
import { Readable } from "stream";
export default async function (request: Koa.Request, response: Koa.Response, ctx: Koa.Context) {
const myReadableData1 = Readable.from("xxx");
await myStorage.put("my-data/style1", myReadableData1);
// The my-data/style1 contentType is set to "binary/octet-stream"
const myReadableData2 = Readable.from("xxx.js");
await myStorage.put("my-data/style2", myReadableData2);
// The my-data/style2 contentType is set to "text/javascript"
return "success";
}