Open6

cloudflare r2 のメモ

GENYAGENYA

[[r2_buckets]]
binding = "好きな名前を付けられる"
bucket_name = "本番のバケット"
preview_bucket_name = "ローカルのバケット"

GENYAGENYA

R2 bucket のTS型

http リクエストメソッド + α っぽい

head
get
put
delete
list


/**
 * An instance of the R2 bucket binding.
 */
interface R2Bucket {
  head(key: string): Promise<R2Object | null>;
  get(key: string): Promise<R2ObjectBody | null>;
  /**
   * Returns R2Object on a failure of the conditional specified in onlyIf.
   */
  get(
    key: string,
    options: R2GetOptions
  ): Promise<R2ObjectBody | R2Object | null>;
  get(
    key: string,
    options?: R2GetOptions
  ): Promise<R2ObjectBody | R2Object | null>;
  put(
    key: string,
    value:
      | ReadableStream
      | ArrayBuffer
      | ArrayBufferView
      | string
      | null
      | Blob,
    options?: R2PutOptions
  ): Promise<R2Object>;
  delete(key: string): Promise<void>;
  list(options?: R2ListOptions): Promise<R2Objects>;
}
GENYAGENYA

list のAPIのオプション https://developers.cloudflare.com/r2/runtime-apis/#r2listoptions

myBucket.list({ limit, include: ['customMetadata'] }) include を使うことで customMetadataと紐付けてデータを引けるようになっている。

If you populate this array, then items returned will include this metadata. A tradeoff is that fewer results may be returned depending on how big this data is. For now the caps are TBD but expect the total memory usage for a list operation may need to be <1MB or even <128kb depending on how many list operations you are sending into one bucket. Make sure to look at truncated for the result rather than having logic like

  limit?: number;
  prefix?: string;
  cursor?: string;
  delimiter?: string;
  include?: ("httpMetadata" | "customMetadata")[];
GENYAGENYA

バケットのファイルをディレクトリでフィルタして取得する

path/to/1.png
path/toto/2.png
path/other/3.png
const list = await r2BindingName.list({ prefix: 'path/to/' }); // 1.pngのオブジェクトだけが返される
GENYAGENYA

miniflare 内だと wrangler.tomlで定義した名称がアクセスできないのはいつか解決したい