Skip to main content

get

The get method is used to retrieve the contents of the object store service with a specified key.

async function get(key: string): Promise<Response | null>;

Reference​

Overview​

You can retrieve the contents of the object store service with the specified key by calling the get method.

import { myStorage } from "#elements";

...

const response: Response = await myStorage.get("my-data/string.txt");
// The following four ways to get content are supported
const arrayBuffer = await response.arrayBuffer();
const text = await response.text();
const blob = await response.blob();
const body = await response.body;

...

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.

Returns​

The get method returns a fetch.Response object.

Examples​

Return the Storage contents​

Here is an example of using the 'Storage.get' method in HTTP Element to retrieve the contents of a given key and return it:

import * as Koa from "koa";
import { myStorage } from "#elements";
export default async function (request: Koa.Request, response: Koa.Response, ctx: Koa.Context) {
const fetchResponse = await myStorage.get(key);
const blob = await fetchResponse! .blob()
response.type = blob.type;
return blob.text();
}

Download Storage content​

Here is an example of using the 'Storage.get' method in HTTP Element to retrieve the contents of a given key and return it:

import * as Koa from "koa";
import { myStorage } from "#elements";
export default async function (request: Koa.Request, response: Koa.Response, ctx: Koa.Context) {
const fetchResponse = await myStorage.get(key);
const blob = await fetchResponse! .blob()
response.type = blob.type;
response.set("Content-Disposition", `attachment; filename=${key}`)
return blob.stream();
}

Get the contents of a large file​

Here's an example of streaming the contents of a given key using the Storage.get method in HTTP Element:

import * as Koa from "koa";
import { myStorage } from "#elements";

export default async function (request: Koa.Request, response: Koa.Response, ctx: Koa.Context) {
const body = (await myStorage.get(key)).body;
const reader = result4.getReader();
const chunks: Buffer[] = [];
let readResult = await reader.read();
while (!readResult.done) {
chunks.push(<Buffer>readResult.value);
readResult = await reader.read();
}
const decoder = new TextDecoder("utf-8");
const strContent = decoder.decode(new Uint8Array(Buffer.concat(chunks)));
return strContent;
}