> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# 共有

> blob.shares で共有リンクを作成、一覧、取り消しし、共有と downloadUrl() リンクのどちらを使うかを選びます。

**共有**は 1 つのオブジェクトへのリンクで、**有効期限**を設定でき、**取り消し**が可能で、**ダウンロード回数**を制限でき、Pro と Enterprise では**パスワード**を要求できます。リンクの動作については[リンクと共有](/ja/blob-reference/links-and-sharing)を参照してください。

## 共有か `downloadUrl()` か

|              | [`downloadUrl()`](/ja/sdks/blob/objects#ダウンロードリンク) | `shares.create()`            |
| ------------ | -------------------------------------------------- | ---------------------------- |
| 公開オブジェクト     | 恒久的な CDN URL (`expires_at: null`)                  | 恒久的な公開 URL にリダイレクト (下の警告を参照) |
| プライベートオブジェクト | 一時リンク、最大 24 時間                                     | 共有リンク、最大 30 日                |
| 取り消し         | **不可**                                             | 可能、`shares.revoke()` で       |
| ダウンロード回数の制限  | なし                                                 | あり、`max_downloads`           |
| パスワード        | なし                                                 | あり、Pro と Enterprise          |

自分で配布する短期間のリンクには `downloadUrl()` を、リンクを取り消す必要がある場合やダウンロードする人を制御したい場合には共有を使います。

## 共有の作成

```typescript theme={"system"}
const share = await blob.shares.create(id, {
    expires_in: 86400,     // seconds, 60 to 2592000 (default 86400)
    max_downloads: 10,     // 1 to 10000
    password: "secret123", // 8 to 128 characters, Pro and Enterprise
});

console.log(share.url);
```

| オプション           | 型        | 説明                                                                |
| --------------- | -------- | ----------------------------------------------------------------- |
| `expires_in`    | `number` | リンクの有効期間 (秒)。60〜2592000 (30 日)。デフォルトは 86400。                      |
| `max_downloads` | `number` | 1〜10000。                                                          |
| `password`      | `string` | 8〜128 文字。Pro と Enterprise のみで、その他のプランでは `UPGRADE_REQUIRED` になります。 |

結果には `id`、`url`、`expires_at`、`max_downloads`、`password` (設定されているかどうか)、`object`、`object_is_public` があります。

<Warning>
  `object_is_public` が `true` の場合、共有リンクはオブジェクトの**恒久的な公開 URL** にリダイレクトされます。ファイルはその URL から引き続きアクセスできるため、パスワード、ダウンロード回数の制限、有効期限は**何も保護しません**。先にオブジェクトをプライベートにしてから共有してください:

  ```typescript theme={"system"}
  const [result] = await blob.update(id, { private: true });
  if (result.ok) {
      id = result.id; // the id changes
      const share = await blob.shares.create(id, { max_downloads: 1 });
  }
  ```
</Warning>

## 共有の一覧

```typescript theme={"system"}
const shares = await blob.shares.list();
```

共有の配列を返します。各共有には `id`、`url`、`object`、`expires_at`、`remaining_downloads`、`password`、`created_at` があります。

## 共有の取り消し

```typescript theme={"system"}
await blob.shares.revoke(share.id);
```

リンクは機能しなくなります。`revoke()` は何も返さずに解決され、存在しない共有を取り消そうとすると `SHARE_NOT_FOUND` で失敗します。

<Tip>
  オブジェクトの削除、移動、名前変更、または公開範囲の変更を行うと、その共有リンクも壊れます。リンクは古い ID を指しているためです。新しい ID に対して新しい共有を作成してください。
</Tip>

<Note>
  `shares.create()` と `shares.revoke()` は **1 回のみ**試行されます。[リトライ](/ja/sdks/blob/errors#リトライポリシー)されるのは、読み取りである `shares.list()` だけです。
</Note>

API リファレンス: [Blob 共有の作成](/ja/blob-reference/endpoint/shares-create)、[Blob 共有の一覧](/ja/blob-reference/endpoint/shares-list)、[Blob 共有の削除](/ja/blob-reference/endpoint/shares-delete)。
