You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+64-13Lines changed: 64 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,16 @@
1
-
An example repo demonstrating image uploading to [R2](https://developers.cloudflare.com/r2/).
1
+
# Upload data to R2
2
2
3
-
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`c3`](https://developers.cloudflare.com/pages/get-started/c3).
3
+
A [Next.js](https://nextjs.org/) example application demonstrating image uploading to [Cloudflare R2](https://developers.cloudflare.com/r2/).
4
+
5
+
This is a Next.js project bootstrapped with [`c3`](https://developers.cloudflare.com/pages/get-started/c3).
6
+
7
+
## Upload Methods
8
+
9
+
This app uses three different methods to upload and download images to an R2 bucket.
10
+
11
+
1.[Workers API](https://developers.cloudflare.com/r2/api/workers/): To use this method, make sure to add the R2 binding in [`wrangler.toml`](./wrangler.toml).
12
+
2.[Presigned URL](https://developers.cloudflare.com/r2/api/s3/presigned-urls/): To use this method, you need Account ID, Access Key ID, and Secret Access Key. Follow the [steps mentioned in the documentation](https://developers.cloudflare.com/r2/api/s3/tokens/) to generate the required credentials.
13
+
3.[Presigned URL using Temporary Credentials](https://developers.cloudflare.com/r2/api/s3/tokens/#temporary-access-credentials): To use this method, you need Account ID, Access Key ID, and Secret Access Key. You also need a personal API Token. Check the [Generating API Token](#configure-credentials-1) section to learn more.
4
14
5
15
## Getting Started
6
16
@@ -26,23 +36,63 @@ If you don't have access to R2, purchase it from your Cloudflare Dashboard.
26
36
27
37
Create a new bucket. Follow the documentation to learn how to [create a new bucket](https://developers.cloudflare.com/r2/get-started/#2-create-a-bucket).
28
38
29
-
### Get credentials
39
+
### For Workers API
30
40
31
-
To access objects from your R2 bucket, you will need the Account ID, Access Key ID, and Secret Access Key. Follow the [steps mentioned in the documentation](https://developers.cloudflare.com/r2/api/s3/tokens/) to generate the required credentials.
41
+
### Add bindings
42
+
43
+
Add the R2 bindings to the `wrangler.toml` file. It should look something like this:
44
+
45
+
```toml
46
+
[[r2_buckets]]
47
+
binding = "IMAGES"
48
+
bucket_name = "my-bucket"
49
+
```
32
50
33
-
### Setup credentials
51
+
> If you update the value of `binding`, make sure you update it in the `env.d.ts`, `app/api/workers-api/upload` and `app/api/workers-api/download` files.
52
+
53
+
### For Presigned URL
54
+
55
+
To use Presigned URL, you need the Account ID, Access Key ID, and Secret Access Key. Follow the [steps mentioned in the documentation](https://developers.cloudflare.com/r2/api/s3/tokens/) to generate the required credentials.
56
+
57
+
#### Configure credentials
34
58
35
59
Rename `.env.copy` to `.env.local`. Paste the credentials you got in the previous step.
36
60
37
-
### Configure CORS
61
+
> For this method, you don't need `AUTH_TOKEN`. You leave it as is or remove it from the `.env.local` file.
62
+
63
+
#### Configure CORS
64
+
65
+
You will need to configure the CORS policies to be able to access the objects. Use the CORS policy available in the `cors.json` file.
66
+
67
+
> **Note:** You might have to update `AllowedOrigins`.
68
+
69
+
You add this CORS policy to your bucket via the Dashboard. You can find the steps to do that in [the documentation](https://developers.cloudflare.com/r2/buckets/cors/#add-cors-policies-from-the-dashboard).
70
+
71
+
### For Presigned URL with Temporary Credentials
72
+
73
+
#### Configure credentials
74
+
75
+
Apart from the Account ID, Access Key ID, and Secret Access Key, you also need an API Token. To create the API Token, follow the instructions mentioned in the [Create API Token documentation](https://developers.cloudflare.com/fundamentals/api/get-started/create-token/).
76
+
77
+
Make sure you have the following permissions:
78
+
79
+
```txt
80
+
Account - Workers R2 Storage:Edit
81
+
All users - API Tokens:Edit
82
+
```
83
+
84
+
Rename `.env.copy` to `.env.local`. Paste the credentials you got in the previous step. For `AUTH_TOKEN` paste the new generated `API Token`.
85
+
86
+
#### Configure CORS
38
87
39
88
You will need to configure the CORS policies to be able to access the objects. Use the CORS policy available in the `cors.json` file.
40
89
41
-
> __Note:__ You might have to update `AllowedOrigins`.
90
+
> **Note:** You might have to update `AllowedOrigins`.
42
91
43
92
You add this CORS policy to your bucket via the Dashboard. You can find the steps to do that in [the documentation](https://developers.cloudflare.com/r2/buckets/cors/#add-cors-policies-from-the-dashboard).
44
93
45
-
### Run development server
94
+
### Run development server
95
+
46
96
Execute the following command to run the development server:
47
97
48
98
```bash
@@ -54,11 +104,12 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the
54
104
## Cloudflare integration
55
105
56
106
Besides the `dev` script mentioned above `c3` has added a few extra scripts that allow you to integrate the application with the [Cloudflare Pages](https://pages.cloudflare.com/) environment, these are:
57
-
-`pages:build` to build the application for Pages using the [`@cloudflare/next-on-pages`](https://github.com/cloudflare/next-on-pages) CLI
58
-
-`preview` to locally preview your Pages application using the [Wrangler](https://developers.cloudflare.com/workers/wrangler/) CLI
59
-
-`deploy` to deploy your Pages application using the [Wrangler](https://developers.cloudflare.com/workers/wrangler/) CLI
60
107
61
-
> __Note:__ while the `dev` script is optimal for local development you should preview your Pages application as well (periodically or before deployments) in order to make sure that it can properly work in the Pages environment (for more details see the [`@cloudflare/next-on-pages` recommended workflow](https://github.com/cloudflare/next-on-pages/blob/05b6256/internal-packages/next-dev/README.md#recommended-workflow))
108
+
-`pages:build` to build the application for Pages using the [`@cloudflare/next-on-pages`](https://github.com/cloudflare/next-on-pages) CLI
109
+
-`preview` to locally preview your Pages application using the [Wrangler](https://developers.cloudflare.com/workers/wrangler/) CLI
110
+
-`deploy` to deploy your Pages application using the [Wrangler](https://developers.cloudflare.com/workers/wrangler/) CLI
111
+
112
+
> **Note:** while the `dev` script is optimal for local development you should preview your Pages application as well (periodically or before deployments) in order to make sure that it can properly work in the Pages environment (for more details see the [`@cloudflare/next-on-pages` recommended workflow](https://github.com/cloudflare/next-on-pages/blob/05b6256/internal-packages/next-dev/README.md#recommended-workflow))
62
113
63
114
### Bindings
64
115
@@ -70,4 +121,4 @@ You can use bindings during development, when previewing locally your applicatio
70
121
71
122
- To use bindings in the preview mode you need to add them to the `pages:preview` script accordingly to the `wrangler pages dev` command. For more details see its [documentation](https://developers.cloudflare.com/workers/wrangler/commands/#dev-1) or the [Pages Bindings documentation](https://developers.cloudflare.com/pages/functions/bindings/).
72
123
73
-
- To use bindings in the deployed application you will need to configure them in the Cloudflare [dashboard](https://dash.cloudflare.com/). For more details see the [Pages Bindings documentation](https://developers.cloudflare.com/pages/functions/bindings/).
124
+
- To use bindings in the deployed application you will need to configure them in the Cloudflare [dashboard](https://dash.cloudflare.com/). For more details see the [Pages Bindings documentation](https://developers.cloudflare.com/pages/functions/bindings/).
0 commit comments