Skip to content
Merged
316 changes: 134 additions & 182 deletions src/content/docs/r2/api/s3/presigned-urls.mdx

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/content/docs/r2/examples/aws/boto3.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,45 @@ Objects:
- cat.png
- todos.txt
```

## Generate presigned URLs

You can also generate presigned links that can be used to share public read or write access to a bucket temporarily.

```python
import boto3

s3 = boto3.client(
service_name="s3",
endpoint_url='https://<accountid>.r2.cloudflarestorage.com',
aws_access_key_id='<access_key_id>',
aws_secret_access_key='<access_key_secret>',
region_name="auto",
)

# Generate presigned URL for reading (GET)
# The ExpiresIn parameter determines how long the presigned link is valid (in seconds)
get_url = s3.generate_presigned_url(
'get_object',
Params={'Bucket': 'my-bucket-name', 'Key': 'dog.png'},
ExpiresIn=3600 # Valid for 1 hour
)

print(get_url)
# https://my-bucket-name.<accountid>.r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...

# Generate presigned URL for writing (PUT)
put_url = s3.generate_presigned_url(
'put_object',
Params={'Bucket': 'my-bucket-name', 'Key': 'dog.png'},
ExpiresIn=3600
)

print(put_url)
```

You can use the link generated by the `put_object` example to upload to the specified bucket and key, until the presigned link expires.

```sh
curl -X PUT "https://my-bucket-name.<accountid>.r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=..." --data-binary @dog.png
```
86 changes: 70 additions & 16 deletions src/content/docs/r2/objects/delete-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
title: Delete objects
pcx_content_type: how-to
sidebar:
order: 3
order: 5
---

import { Render, DashButton } from "~/components";
import { Render, Tabs, TabItem, DashButton } from "~/components";

You can delete objects from your bucket from the Cloudflare dashboard or using the Wrangler.
You can delete objects from R2 using the dashboard, Workers API, S3 API, or command-line tools.

## Delete objects via the Cloudflare dashboard
## Delete via dashboard

1. In the Cloudflare dashboard, go to the **R2 object storage** page.

Expand All @@ -19,26 +19,80 @@ You can delete objects from your bucket from the Cloudflare dashboard or using t
4. Select your objects and select **Delete**.
5. Confirm your choice by selecting **Delete**.

## Delete objects via Wrangler
## Delete via Workers API

:::caution
Use R2 [bindings](/workers/runtime-apis/bindings/) in Workers to delete objects:

Deleting objects from a bucket is irreversible.
```ts ins={3}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
await env.MY_BUCKET.delete("image.png");
return new Response("Deleted");
},
} satisfies ExportedHandler<Env>;
```

:::
For complete documentation, refer to [Workers API](/r2/api/workers/workers-api-usage/).

You can delete an object directly by calling `delete` against a `{bucket}/{path/to/object}`.
## Delete via S3 API

For example, to delete the object `foo.png` from bucket `test-bucket`:
Use S3-compatible SDKs to delete objects. You'll need your [account ID](/fundamentals/account/find-account-and-zone-ids/) and [R2 API token](/r2/api/tokens/).

```sh
wrangler r2 object delete test-bucket/foo.png
<Tabs>
<TabItem label="JavaScript">

```ts
import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3";

const S3 = new S3Client({
region: "auto",
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_ACCESS_KEY,
},
});

await S3.send(
new DeleteObjectCommand({
Bucket: "my-bucket",
Key: "image.png",
}),
);
```

```sh output
</TabItem>
<TabItem label="Python">

```python
import boto3

Deleting object "foo.png" from bucket "test-bucket".
Delete complete.
s3 = boto3.client(
service_name="s3",
endpoint_url=f"https://{account_id}.r2.cloudflarestorage.com",
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
region_name="auto",
)

s3.delete_object(Bucket="my-bucket", Key="image.png")
```

<Render file="link-to-workers-r2-api" product="r2"/>
</TabItem>
</Tabs>

For complete S3 API documentation, refer to [S3 API](/r2/api/s3/api/).

## Delete via Wrangler

:::caution

Deleting objects from a bucket is irreversible.

:::

Use [Wrangler](/workers/wrangler/install-and-update/) to delete objects. Run the [`r2 object delete` command](/workers/wrangler/commands/#r2-object-delete):

```sh
wrangler r2 object delete test-bucket/image.png
```
98 changes: 82 additions & 16 deletions src/content/docs/r2/objects/download-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,103 @@
title: Download objects
pcx_content_type: how-to
sidebar:
order: 2
order: 4
---

import { Render, DashButton } from "~/components";
import { Render, Tabs, TabItem, DashButton } from "~/components";

You can download objects from your bucket from the Cloudflare dashboard or using the Wrangler.
You can download objects from R2 using the dashboard, Workers API, S3 API, or command-line tools.

## Download objects via the Cloudflare dashboard
## Download via dashboard

1. In the Cloudflare dashboard, go to the **R2 object storage** page.

<DashButton url="/?to=/:account/r2/overview" />
2. Locate and select your bucket.
2. Select your bucket.
3. Locate the object you want to download.
4. At the end of the object's row, select the menu button and click **Download**.
4. Select **...** for the object and click **Download**.

## Download objects via Wrangler
## Download via Workers API

You can download objects from a bucket, including private buckets in your account, directly.
Use R2 [bindings](/workers/runtime-apis/bindings/) in Workers to download objects:

For example, to download `file.bin` from `test-bucket`:
```ts ins={3}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const object = await env.MY_BUCKET.get("image.png");
return new Response(object.body);
},
} satisfies ExportedHandler<Env>;
```

```sh
wrangler r2 object get test-bucket/file.bin
For complete documentation, refer to [Workers API](/r2/api/workers/workers-api-usage/).

## Download via S3 API

Use S3-compatible SDKs to download objects. You'll need your [account ID](/fundamentals/account/find-account-and-zone-ids/) and [R2 API token](/r2/api/tokens/).

<Tabs>
<TabItem label="JavaScript">

```ts
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";

const S3 = new S3Client({
region: "auto",
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_ACCESS_KEY,
},
});

const response = await S3.send(
new GetObjectCommand({
Bucket: "my-bucket",
Key: "image.png",
}),
);
```

```sh output
Downloading "file.bin" from "test-bucket".
Download complete.
</TabItem>
<TabItem label="Python">

```python
import boto3

s3 = boto3.client(
service_name="s3",
endpoint_url=f"https://{account_id}.r2.cloudflarestorage.com",
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
region_name="auto",
)

response = s3.get_object(Bucket="my-bucket", Key="image.png")
image_data = response["Body"].read()
```

The file will be downloaded into the current working directory. You can also use the `--file` flag to set a new name for the object as it is downloaded, and the `--pipe` flag to pipe the download to standard output (stdout).
</TabItem>
</Tabs>

For complete S3 API documentation, refer to [S3 API](/r2/api/s3/api/).

### Presigned URLs

For client-side downloads where users download directly from R2, use presigned URLs. Your server generates a temporary download URL that clients can use without exposing your API credentials.

1. Your application generates a presigned GET URL using an S3 SDK
2. Send the URL to your client
3. Client downloads directly from R2 using the presigned URL

For details on generating and using presigned URLs, refer to [Presigned URLs](/r2/api/s3/presigned-urls/).

## Download via Wrangler

Use [Wrangler](/workers/wrangler/install-and-update/) to download objects. Run the [`r2 object get` command](/workers/wrangler/commands/#r2-object-get):

```sh
wrangler r2 object get test-bucket/image.png
```

<Render file="link-to-workers-r2-api" product="r2"/>
The file will be downloaded into the current working directory. You can also use the `--file` flag to set a new name for the object as it is downloaded, and the `--pipe` flag to pipe the download to standard output (stdout).
2 changes: 1 addition & 1 deletion src/content/docs/r2/objects/multipart-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Multipart upload
pcx_content_type: concept
sidebar:
order: 1
order: 3

---

Expand Down
Loading