Skip to content

Commit b956d7b

Browse files
address feedback, align some wording
1 parent fb4167d commit b956d7b

File tree

1 file changed

+98
-55
lines changed
  • src/pages/[platform]/build-a-backend/storage/use-with-custom-s3

1 file changed

+98
-55
lines changed

src/pages/[platform]/build-a-backend/storage/use-with-custom-s3/index.mdx

Lines changed: 98 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ The policy will look something like this:
5252
"Statement": [
5353
{
5454
"Sid": "Statement1",
55-
"Principal": { "AWS": "arn:aws:iam::<AWS-account-ID>:role/<role-name>" },
55+
"Principal": {
56+
"AWS": "arn:aws:iam::<AWS-account-ID>:role/<role-name>"
57+
},
5658
"Effect": "Allow",
5759
"Action": [
5860
"s3:PutObject",
@@ -61,7 +63,7 @@ The policy will look something like this:
6163
"s3:ListBucket"
6264
],
6365
"Resource": [
64-
"arn:aws:s3:::<bucket-name>/",
66+
"arn:aws:s3:::<bucket-name>",
6567
"arn:aws:s3:::<bucket-name>/*"
6668
]
6769
}
@@ -73,35 +75,34 @@ Replace `<AWS-account-ID>` with your AWS account ID and `<role-name>` with the I
7375
You can refer to [Amazon S3's Policies and Permissions documentation](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-policy-language-overview.html) for more ways to customize access to the bucket.
7476

7577
<Callout warning>
76-
In order to make calls to your S3 bucket from your application, you must also set up a CORS Policy for your S3 bucket. This applies only to manually-configured S3 buckets. Learn more about [setting up a CORS Policy for your S3 bucket](/[platform]/build-a-backend/storage/extend-s3-resources/#for-manually-configured-s3-resources).
78+
In order to make calls to your manually configured S3 bucket from your application, you must also set up a [CORS Policy](/[platform]/build-a-backend/storage/extend-s3-resources/#for-manually-configured-s3-resources) for the bucket.
7779
</Callout>
7880

7981
### Specify S3 bucket in Amplify's backend config
8082

81-
Next, use the `addOutput` method from the backend definition object to define a custom S3 bucket by specifying the name and region of the bucket in your **amplify/backend.ts** file. More options can be specified if more granular control over your custom S3 bucket is needed.
82-
83-
Afterwards, set up the appropriate resources and IAM policies to be attached to the backend.
83+
Next, use the `addOutput` method from the backend definition object to define a custom S3 bucket by specifying the name and region of the bucket in your `amplify/backend.ts` file. You must also set up the appropriate resources and IAM policies to be attached to the backend.
8484

8585
<Callout>
8686

87-
**Important**
88-
89-
You cannot use both a storage backend configured through Amplify and a custom S3 bucket at the same time.
87+
**Important:** You cannot use both a storage backend configured through Amplify and a custom S3 bucket at the same time.
9088

9189
If you specify a custom S3 bucket, no sandbox storage resource will be created. The provided custom S3 bucket will be used, even in the sandbox environment.
9290

9391
</Callout>
9492

95-
Below is an example of configuring the backend to define a custom S3 bucket where only authenticated (i.e. signed in) users have full access to a folder called `public/`:
93+
Below are several examples of configuring the backend to define a custom S3 bucket:
9694

95+
<BlockSwitcher>
96+
<Block name="Authenticated Users">
97+
Below is an example of expanding the original backend object to grant all authenticated (i.e. signed in) users with full access to files under `public/`:
9798
```ts title="amplify/backend.ts"
9899
import { defineBackend } from "@aws-amplify/backend";
99100
import { auth } from "./auth/resource";
100101

101102
const backend = defineBackend({
102103
auth,
103104
});
104-
//highlight-start
105+
// highlight-start
105106
backend.addOutput({
106107
storage: {
107108
aws_region: "<region>",
@@ -112,8 +113,10 @@ backend.addOutput({
112113
aws_region: "<region>",
113114
bucket_name: "<bucket-name>",
114115
name: "<bucket-name>",
115-
// optional: `paths` can be used to set up access to specific bucket prefixes,
116-
// and configure user access types to those prefixes
116+
/*
117+
optional: `paths` can be used to set up access to specific
118+
bucket prefixes and configure user access types to them
119+
*/
117120
paths: {
118121
"public/*": {
119122
authenticated: ["get", "list", "write", "delete"],
@@ -124,13 +127,19 @@ backend.addOutput({
124127
},
125128
});
126129

127-
// Define an inline policy to attach to Amplify's auth role
128-
// This policy defines how authenticated users can access your existing bucket
130+
/*
131+
Define an inline policy to attach to Amplify's auth role
132+
This policy defines how authenticated users can access your existing bucket
133+
*/
129134
const authPolicy = new Policy(backend.stack, "customBucketAuthPolicy", {
130135
statements: [
131136
new PolicyStatement({
132137
effect: Effect.ALLOW,
133-
actions: ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
138+
actions: [
139+
"s3:GetObject",
140+
"s3:PutObject",
141+
"s3:DeleteObject"
142+
],
134143
resources: ["arn:aws:s3:::<bucket-name>/public/*",],
135144
}),
136145
new PolicyStatement({
@@ -153,20 +162,17 @@ const authPolicy = new Policy(backend.stack, "customBucketAuthPolicy", {
153162
backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(authPolicy);
154163
// highlight-end
155164
```
156-
157-
From there, you can further configure the backend to add custom authorization rules for different user types.
158-
159-
<Callout>
160-
The custom authorization rules defined in the examples below are able to be combined and follow the same rules used when working with Amplify-defined storage. For more information about the access types and access definition rules supported by Amplify, please refer to our documentation on [customizing authorization rules](/[platform]/build-a-backend/storage/authorization/).
161-
</Callout>
162-
163-
<BlockSwitcher>
165+
</Block>
164166
<Block name="Guest Users">
165167
Below is an example of expanding the original backend object to grant all guest (i.e. not signed in) users read access to files under `public/`:
166168

167169
```ts title="amplify/backend.ts"
170+
import { defineBackend } from "@aws-amplify/backend";
171+
import { auth } from "./auth/resource";
168172

169-
// ...
173+
const backend = defineBackend({
174+
auth,
175+
});
170176

171177
backend.addOutput({
172178
storage: {
@@ -192,10 +198,11 @@ backend.addOutput({
192198
});
193199

194200
// ... Authenticated user policy and role attachment goes here ...
195-
196201
// highlight-start
197-
// Define an inline policy to attach to Amplify's un-auth role
198-
// This policy defines how un-authenticated users/guests can access your existing bucket
202+
/*
203+
Define an inline policy to attach to Amplify's un-auth role
204+
This policy defines how unauthenticated/guest users can access your existing bucket
205+
*/
199206
const unauthPolicy = new Policy(backend.stack, "customBucketUnauthPolicy", {
200207
statements: [
201208
new PolicyStatement({
@@ -227,11 +234,15 @@ backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(
227234
```
228235
</Block>
229236
<Block name="User Groups">
230-
Below is an example of expanding the original backend object to have an `admin/` folder that only users belonging to the "admin" user group can manage:
237+
Below is an example of expanding the original backend object to have an `admin/` folder that authenticated users can read, but only users belonging to the "admin" user group can manage:
231238
{/* cSpell:disable */}
232239
```ts title="amplify/backend.ts"
240+
import { defineBackend } from "@aws-amplify/backend";
241+
import { auth } from "./auth/resource";
233242

234-
// ...
243+
const backend = defineBackend({
244+
auth,
245+
});
235246

236247
backend.addOutput({
237248
storage: {
@@ -242,14 +253,17 @@ backend.addOutput({
242253
aws_region: "<region>",
243254
bucket_name: "<bucket-name>",
244255
name: "<bucket-name>",
245-
// @ts-expect-error: Amplify backend type issue
246-
// https://github.com/aws-amplify/amplify-backend/issues/2569
256+
/*
257+
@ts-expect-error: Amplify backend type issue
258+
https://github.com/aws-amplify/amplify-backend/issues/2569
259+
*/
247260
paths: {
248261
"public/*": {
249262
authenticated: ["get", "list", "write", "delete"],
250263
},
251264
// highlight-start
252265
"admin/*": {
266+
authenticated: ["get", "list"],
253267
groupsadmin: ["get", "list", "write", "delete"],
254268
},
255269
// highlight-end
@@ -260,15 +274,21 @@ backend.addOutput({
260274
});
261275

262276
// ... Authenticated user policy and role attachment goes here ...
263-
264277
// highlight-start
265-
// Define an inline policy to attach to "admin" user group role
266-
// This policy defines how authenticated users with "admin" user group role can access your existing bucket
278+
/*
279+
Define an inline policy to attach to "admin" user group role
280+
This policy defines how authenticated users with
281+
"admin" user group role can access your existing bucket
282+
*/
267283
const adminPolicy = new Policy(backend.stack, "customBucketAdminPolicy", {
268284
statements: [
269285
new PolicyStatement({
270286
effect: Effect.ALLOW,
271-
actions: ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
287+
actions: [
288+
"s3:GetObject",
289+
"s3:PutObject",
290+
"s3:DeleteObject"
291+
],
272292
resources: ["arn:aws:s3:::<bucket-name>/admin/*"],
273293
}),
274294
new PolicyStatement({
@@ -294,14 +314,18 @@ backend.auth.resources.groups["admin"].role.attachInlinePolicy(adminPolicy);
294314
{/* cSpell:enable */}
295315
</Block>
296316
<Block name="Owners">
297-
While Amplify Storage uses the reserved token `entity_id` to represent the user's identity ID, that token is not available when configuring external buckets. Instead, you must use [the IAM role `${cognito-identity.amazonaws.com:sub}`](https://docs.aws.amazon.com/cognito/latest/developerguide/iam-roles.html#trust-policies-classic) to specify the user's identity ID.
317+
Amplify allows scoping file access to individual users via the user's identity ID. To specify the user's identity ID, you can use the token `${cognito-identity.amazonaws.com:sub}`.
298318

299319
Below is an example of expanding the original backend object to define read access for guests to the `public/` folder, as well as defining a `protected/` folder where anyone can view uploaded files, but only the file owner can modify/delete them:
300320

301321
{/* cSpell:disable */}
302322
```ts title="amplify/backend.ts"
323+
import { defineBackend } from "@aws-amplify/backend";
324+
import { auth } from "./auth/resource";
303325

304-
// ...
326+
const backend = defineBackend({
327+
auth,
328+
});
305329

306330
backend.addOutput({
307331
storage: {
@@ -312,20 +336,22 @@ backend.addOutput({
312336
aws_region: "<region>",
313337
bucket_name: "<bucket-name>",
314338
name: "<bucket-name>",
315-
// @ts-expect-error: Amplify backend type issue
316-
// https://github.com/aws-amplify/amplify-backend/issues/2569
339+
/*
340+
@ts-expect-error: Amplify backend type issue
341+
https://github.com/aws-amplify/amplify-backend/issues/2569
342+
*/
317343
paths: {
318344
"public/*": {
319345
guest: ["get", "list"],
320346
authenticated: ["get", "list", "write", "delete"],
321347
},
322348
// highlight-start
323-
// allow guests and authenticated users to view all folders and files within `protected/`
349+
// allow all users to view all folders/files within `protected/`
324350
"protected/*": {
325351
guest: ["get", "list"],
326352
authenticated: ["get", "list"],
327353
},
328-
// allow owners full access to modify/delete their own files in their assigned subfolder
354+
// allow owners to get/modify/delete their own files in assigned subfolder
329355
"protected/${cognito-identity.amazonaws.com:sub}/*": {
330356
entityidentity: ["get", "list", "write", "delete"]
331357
}
@@ -338,8 +364,11 @@ backend.addOutput({
338364

339365
// highlight-start
340366

341-
// Define an inline policy to attach to Amplify's un-auth role
342-
// This policy defines how unauthenticated users/guests can access your existing bucket
367+
/*
368+
Define an inline policy to attach to Amplify's un-auth role
369+
This policy defines how unauthenticated users/guests
370+
can access your existing bucket
371+
*/
343372
const unauthPolicy = new Policy(backend.stack, "customBucketUnauthPolicy", {
344373
statements: [
345374
new PolicyStatement({
@@ -371,9 +400,11 @@ const unauthPolicy = new Policy(backend.stack, "customBucketUnauthPolicy", {
371400
],
372401
});
373402

374-
// Define an inline policy to attach to Amplify's auth role
375-
// This policy defines how authenticated users can access your existing bucket
376-
// and customizes owner access to their individual folder and any files inside
403+
/*
404+
Define an inline policy to attach to Amplify's auth role
405+
This policy defines how authenticated users can access your
406+
existing bucket and customizes owner access to their individual folder
407+
*/
377408
const authPolicy = new Policy(backend.stack, "customBucketAuthPolicy", {
378409
statements: [
379410
new PolicyStatement({
@@ -413,7 +444,9 @@ const authPolicy = new Policy(backend.stack, "customBucketAuthPolicy", {
413444
new PolicyStatement({
414445
effect: Effect.ALLOW,
415446
actions: ["s3:DeleteObject"],
416-
resources: ["arn:aws:s3:::<bucket-name>/protected/${cognito-identity.amazonaws.com:sub}/*"],
447+
resources: [
448+
"arn:aws:s3:::<bucket-name>/protected/${cognito-identity.amazonaws.com:sub}/*"
449+
],
417450
}),
418451
],
419452
});
@@ -430,21 +463,26 @@ backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(authPolicy);
430463
{/* cSpell:enable */}
431464
</Block>
432465
</BlockSwitcher>
466+
467+
<Callout>
468+
The custom authorization rules defined in the examples are able to be combined, and follow the same rules used when working with Amplify-defined storage. For more information about the access types and access definition rules supported by Amplify, please refer to our documentation on [customizing authorization rules](/[platform]/build-a-backend/storage/authorization/).
469+
</Callout>
470+
433471
<InlineFilter filters={["javascript", "nextjs", "react", "angular", "vue", "react-native", "android", "swift"]}>
434472

435-
### Import latest amplify_outputs.json file
473+
### Import latest `amplify_outputs.json` file
436474

437-
To ensure the local **amplify_outputs.json** file is up-to-date, you can run [the npx ampx generate outputs command](/[platform]/reference/cli-commands/#npx-ampx-generate-outputs) or download the latest **amplify_outputs.json** from the Amplify console as shown below.
475+
To ensure the local `amplify_outputs.json` file is up-to-date, you can run [the `npx ampx generate outputs` command](/[platform]/reference/cli-commands/#npx-ampx-generate-outputs) or download the latest `amplify_outputs.json` from the Amplify console as shown below.
438476

439477
![](/images/gen2/getting-started/react/amplify-outputs-download.png)
440478

441479
</InlineFilter>
442480

443481
<InlineFilter filters={["flutter"]}>
444482

445-
### Import latest amplify_outputs.dart file
483+
### Import latest `amplify_outputs.dart` file
446484

447-
To ensure the local **amplify_outputs.dart** file is up-to-date, you can run [the npx ampx generate outputs command](/[platform]/reference/cli-commands/#npx-ampx-generate-outputs).
485+
To ensure the local `amplify_outputs.dart` file is up-to-date, you can run [the `npx ampx generate outputs` command](/[platform]/reference/cli-commands/#npx-ampx-generate-outputs).
448486

449487
</InlineFilter>
450488

@@ -453,11 +491,11 @@ Now that you've configured the necessary permissions, you can start using the st
453491

454492
## Use storage resources without an Amplify backend
455493

456-
While using the Amplify Backend is the easiest way to get started, existing storage resources can also be integrated with Amplify Storage.
494+
While using the Amplify backend is the easiest way to get started, existing storage resources can also be integrated with Amplify Storage.
457495

458496
In addition to manually configuring your storage options, you will also need to ensure Amplify Auth is properly configured in your project and associated IAM roles have the necessary permissions to interact with your existing bucket. Read more about [using existing auth resources without an Amplify backend](/[platform]/build-a-backend/auth/use-existing-cognito-resources/#use-auth-resources-without-an-amplify-backend).
459497

460-
### Using Amplify configure
498+
### Using `Amplify.configure`
461499
Existing storage resource setup can be accomplished by passing the resource metadata to `Amplify.configure`. This will configure the Amplify Storage client library to interact with the additional resources. It's recommended to add the Amplify configuration step as early as possible in the application lifecycle, ideally at the root entry point.
462500

463501
{/* cSpell:disable */}
@@ -492,6 +530,7 @@ Amplify.configure({
492530
entityidentity: ["get", "list", "write", "delete"]
493531
},
494532
"admin/*": {
533+
authenticated: ["get", "list"],
495534
groupsadmin: ["get", "list", "write", "delete"],
496535
},
497536
}
@@ -510,7 +549,7 @@ Amplify.configure({
510549
```
511550
{/* cSpell:enable */}
512551

513-
### Using Amplify outputs
552+
### Using `amplify_outputs.json`
514553

515554
Alternatively, existing storage resources can be used by creating or modifying the `amplify_outputs.json` file directly.
516555

@@ -573,6 +612,10 @@ Alternatively, existing storage resources can be used by creating or modifying t
573612
]
574613
},
575614
"admin/*": {
615+
"authenticated": [
616+
"get",
617+
"list"
618+
],
576619
"groupsadmin": [
577620
"get",
578621
"list",

0 commit comments

Comments
 (0)