Skip to content

[StorageBrowser] integrating existing S3 resources issue #6258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
calebpollman opened this issue Dec 18, 2024 · 9 comments
Open

[StorageBrowser] integrating existing S3 resources issue #6258

calebpollman opened this issue Dec 18, 2024 · 9 comments
Labels

Comments

@calebpollman
Copy link
Member

Hi, I'm having a bit of trouble to make it work with existing Cognito and S3 resources.
I've referenced the resources on backend configuration (code below), the bucket also has CORS configured according to the documentation: https://ui.docs.amplify.aws/react/connected-components/storage/storage-browser#bucket-cors

And I also set bucket policies to allow access from the Coginito Authenticated role to access and list the bucket.
However, I'm getting on display that there is "No folders or files"
I was able to make it work, if I edit amplify_outputs.json directly and add path configuration access to " * / * "

backend.addOutput({
  auth: {
    aws_region: BACKEND_CONFIG.REGION,
    user_pool_id: BACKEND_CONFIG.COGNITO_USER_POOL_ID,
    user_pool_client_id: BACKEND_CONFIG.COGNITO_USER_POOL_CLIENT_ID,
    identity_pool_id: BACKEND_CONFIG.COGNITO_IDENTITY_POOL_ID,
    username_attributes: ['username'],
    user_verification_types: ['email'],
    mfa_configuration: 'OPTIONAL',
    unauthenticated_identities_enabled: true,
    standard_required_attributes: ['email', 'name'],
    password_policy: {...}
  },
  storage: {
    aws_region: BACKEND_CONFIG.REGION,
    bucket_name: BACKEND_CONFIG.BUCKET
  }
});

Here is my bucket policy

{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Principal": {
				"AWS": [
					"<ARN-COGNITO-ROLE-AUTHENTICATED>"
				]
			},
			"Action": [
				"s3:GetObject",
				"s3:PutObject",
				"s3:DeleteObject"
			],
			"Resource": "arn:aws:s3:::<BUCKET-NAME>/*"
		},
		{
			"Effect": "Allow",
			"Principal": {
				"AWS": [
					"<ARN-COGNITO-ROLE-AUTHENTICATED>"
				]
			},
			"Action": "s3:ListBucket",
			"Resource": "arn:aws:s3:::<BUCKET-NAME>/*"
		}
	]
}

Originally posted by @alanst32 in #5731 (comment)

@github-actions github-actions bot added the pending-triage Issue is pending triage label Dec 18, 2024
@cwomack cwomack added StorageBrowser feature-request Request a new feature and removed pending-triage Issue is pending triage labels Dec 20, 2024
@AllanZhengYP
Copy link
Member

Regarding this issue of supporting existing S3 resources, I can confirm StoBro supports listing from bucket root.

So I think to support S3 bucket's that's not created by Amplify, but still configured to be accessed by Cognito identity,
the only gap I can see is to have backend.addOutput to support /* path.

@nookale
Copy link

nookale commented Feb 4, 2025

Hi, any news? I am also unable to use Amplify Storage with any S3 bucket (https://docs.amplify.aws/react/build-a-backend/storage/use-with-custom-s3/#use-storage-resources-with-an-amplify-backend)

Thanks!

@github-actions github-actions bot added the pending-maintainer-response Issue is pending response from an Amplify UI maintainer label Feb 4, 2025
@calebpollman
Copy link
Member Author

Hi @nookale! Can you provide some additional information on how you've configured the StorageBrowser?

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify UI maintainer label Feb 4, 2025
@itconsultor
Copy link

itconsultor commented Feb 5, 2025

Hello @calebpollman can you please share how you configured this part: ".....I was able to make it work, if I edit amplify_outputs.json directly and add path configuration access to " * / * "....." Since I was checking the amplify_outputs.json file, but I don't see the "path Configuration" section there.

Thanks advance!

Carlos

@github-actions github-actions bot added the pending-maintainer-response Issue is pending response from an Amplify UI maintainer label Feb 5, 2025
@nookale
Copy link

nookale commented Feb 7, 2025

Hi @nookale! Can you provide some additional information on how you've configured the StorageBrowser?

From this page: https://docs.amplify.aws/react/build-a-backend/storage/use-with-custom-s3/

I tried the part: Use storage resources with an Amplify backend.

Regarding the bucket, besides the one specified in the article I mentioned above, I also used:

  • Bucket Policy:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::<s3-bucket-name>",
                "arn:aws:s3:::<s3-bucket-name>/*"
            ],
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<account-number>:role/<amplify-role-associated-with-the-project>"
            },
            "Action": [
                "s3:PutBucketPolicy",
                "s3:GetBucket*",
                "s3:List*",
                "s3:DeleteObject*"
            ],
            "Resource": [
                "arn:aws:s3:::<s3-bucket-name>",
                "arn:aws:s3:::<s3-bucket-name>/*"
            ]
        }
    ]
}
  • CORS:
[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "x-amz-server-side-encryption",
            "x-amz-request-id",
            "x-amz-id-2",
            "ETag"
        ],
        "MaxAgeSeconds": 3000
    }
]

Which is the configuration that is automatically generated when a bucket is created as an Amplified resource, so I copied it to the "non-amplify" bucket I wanted to use with Storage. However, I tried many different combinations and modifications for both the bucket policy and CORS. None of them worked.

Also, regarding the "Principal," it was unclear which one to use, so I tried all the roles associated with the amplify project: CustomS3AutoDeleteObjects, AmplifyBranchLinkerCustom, amplifyAuthunauthenticate, amplifyAuthauthenticatedU.

For CORS, I have also used this (https://ui.docs.amplify.aws/react/connected-components/storage/storage-browser):


[
  {
    "ID": "S3CORSRuleId1",
    "AllowedHeaders": [
      "*"
    ],
    "AllowedMethods": [
      "GET",
      "HEAD",
      "PUT",
      "POST",
      "DELETE"
    ],
    "AllowedOrigins": [
      "*"
    ],
    "ExposeHeaders": [
      "last-modified",
      "content-type",
      "content-length",
      "etag",
      "x-amz-version-id",
      "x-amz-request-id",
      "x-amz-id-2",
      "x-amz-cf-id",
      "x-amz-storage-class",
      "date",
      "access-control-expose-headers"
    ],
    "MaxAgeSeconds": 3000
  }
]

Regarding the Next Js app, I followed the instructions that can be found here: https://docs.amplify.aws/react/build-a-backend/storage/use-with-custom-s3/

@nookale
Copy link

nookale commented Feb 7, 2025

By the way, just for the records and in case it may be of interest:

When the bucket is created as an Amplify Resource, the storage part of the autogenerated amplify_outputs.json file looks like:

  "storage": {
    "aws_region": "<region>",
    "bucket_name": "<bucket-name>",
    "buckets": [
      {
        "name": "<your-additional-bucket-friendly-name>",
        "bucket_name": "<bucket-name>",
        "aws_region": "<region>"
      }
    ]
  }

However, when adding a custom S3 bucket as a resource, this is how it looks like:

  "storage": {
    "aws_region": "<region>",
    "bucket_name": "<bucket-name>"
  }

@sephilink
Copy link

Hi :)
Any news on this one ? it seems it should be possible as per the documentation but I couldn't make it work properly, especially while adding groups control

@AllanZhengYP
Copy link
Member

@sephilink

Have you tried adding the paths to your configuration(these is a bug with this approach as you can see, and we are fixing it right now):

backend.addOutput({
  storage: {
    bucket_name: 'my-default-bucket',
    aws_region: 'my-default-bucket-region',
    buckets: [{
      name: 'default-bucket-friendly-name',
      aws_region: 'my-default-bucket-region',
      // @ts-expect-error
      paths: {
        'my-root-folder/*': {
          guest: ['get', 'list'] as const,
          authenticated: ['list', 'get', 'delete', 'write'] as const,
        }
      }
    }]
  }
});

You need to use paths to let the StorageBrowser know what to render in the locations view(1st screen). The path name should match your cognito permission.

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify UI maintainer label Mar 18, 2025
@tiffanynwyeung
Copy link
Member

Hello @sephilink, @nookale, @itconsultor et al, we've updated our documentation on integrating existing S3 buckets with Amplify, including several examples on how to configure your bucket with the Amplify backend. Please let us know if you're still encountering issues with setting up the bucket, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants