@edgefirst-dev/r2-file-storage - v1.0.2
    Preparing search index...

    Class R2FileStorage

    A FileStorage that is backed by an R2 bucket.

    Implements

    • FileStorage
    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    r2: R2Bucket

    The R2Bucket bindings to use

    Methods

    • Get a File at the given key.

      Parameters

      • key: string

        The key to look up

      Returns Promise<null | File>

      The file with the given key, or null if no such key exists

    • Check if a file with the given key exists.

      Parameters

      • key: string

        The key to look up

      Returns Promise<boolean>

      true if a file with the given key exists, false otherwise

    • List the files in storage.

      The following options are available:

      • cursor: An opaque string that allows you to paginate over the keys in storage
      • includeMetadata: If true, include file metadata in the result
      • limit: The maximum number of files to return
      • prefix: Only return keys that start with this string

      For example, to list all files under keys that start with user123/:

      let result = await storage.list({ prefix: 'user123/' });
      console.log(result.files);
      // [
      // { key: "user123/..." },
      // { key: "user123/..." },
      // ...
      // ]

      result.files will be an array of { key: string } objects. To include metadata about each file, use includeMetadata: true.

      let result = await storage.list({ prefix: 'user123/', includeMetadata: true });
      console.log(result.files);
      // [
      // {
      // key: "user123/...",
      // lastModified: 1737955705270,
      // name: "hello.txt",
      // size: 16,
      // type: "text/plain"
      // },
      // ...
      // ]

      Pagination is done via an opaque cursor property in the list result object. If it is not undefined, there are more files to list. You can list them by passing the cursor back in the options object on the next call.

      let result = await storage.list();

      console.log(result.files);

      if (result.cursor !== undefined) {
      let result2 = await storage.list({ cursor: result.cursor });
      }

      Use the limit option to limit how many results you get back in the files array.

      Type Parameters

      • T extends ListOptions

      Parameters

      • Optionaloptions: T

        Options for the list operation

      Returns Promise<ListResult<T>>

      An object with an array of files and an optional cursor property

    • Put a File in storage and return a new file backed by this storage.

      Parameters

      • key: string

        The key to store the file under

      • file: File

        The file to store

      Returns Promise<File>

      A new File object backed by this storage

    • Remove the file with the given key from storage.

      Parameters

      • key: string

        The key to remove

      Returns Promise<void>

      A promise that resolves when the file has been removed

    • Put a File in storage at the given key.

      Parameters

      • key: string

        The key to store the file under

      • file: File

        The file to store

      Returns Promise<void>

      A promise that resolves when the file has been stored