Amplify Gen 2 uses default S3 bucket instead of selected one

0

I am building an app in react native using amplify. I have 2 S3 buckets, one for images and the other for thumbnails which i have set up through amplify configure.

Amplify.configure({ Auth: { //auth settings }, Storage: { S3: { region: "ap-southeast-2", bucket: "app-thumbnails",

  buckets: {
    'thumbnails': {
      bucketName: 'app-thumbnails',
      region: 'ap-southeast-2'
    },
    'images': {
      bucketName: 'app-images',
      region: 'ap-southeast-2'
    }
  }
}

}, });

When trying to access files from the images bucket using options it switches to the default bucket. I have tried switching the default bucket in the config and experience the same result when trying to access thumbnails.

import { list } from 'aws-amplify/storage';

useEffect(() => {
    const getImages = async () => {
      try {
        const result = await list({
            path: '',
            options: {
                bucket: 'images'
            }
        });
        setThumbnails(result);
        console.log(result);
      } catch (error) {
        console.log( error);
      }
    };

    getImages();
  }, []);

I followed the Gen 2 documentation for react native and still run into this error. Any help would be greatly appreciated.

질문됨 3달 전94회 조회
2개 답변
0

Alternatively, provide bucket name from console and associated region:

import { list } from 'aws-amplify/storage';

useEffect(() => {
    const getImages = async () => {
      try {
        const result = await list({
            path: '',
            options: {
                bucket: {
                              bucketName: 'app-images',
                              region: 'ap-southeast-2'
                        }
            }
        });
        setThumbnails(result);
        console.log(result);
      } catch (error) {
        console.log( error);
      }
    };

    getImages();
  }, []);

For reference: https://docs.amplify.aws/react-native/build-a-backend/storage/list-files/#list-files-from-a-specified-bucket

AWS
답변함 3달 전
  • Hi Alex,

    Thanks for your reply, this is the method I've tried, exactly as its done in the documentation with the bucketName and region in the options however it still goes to the default bucket from my amplify.configure. I have tested both buckets i am using and have the required access however for some reason it looks like the options section is maybe not being picked up? I have set up my amplify.configure as shown in the documentation here: https://docs.amplify.aws/react-native/build-a-backend/storage/use-with-custom-s3/

    Thanks for your time

0

Hi Nem:

Please try to create named backend objects :

amplify/storage/resource.ts

export const appThumbnails = defineStorage({
  name: 'appThumbnails',
  isDefault: true, // identify your default storage bucket (required)
});

export const appImages = defineStorage({
  name: 'appImages',
  access: (allow) => ({
    'private/{entity_id}/*': [
      allow.entity('identity').to(['read', 'write', 'delete'])
    ]
  })
})

amplify/backend.ts

import { defineBackend } from '@aws-amplify/backend';
import { auth } from './auth/resource';
import { appThumbnails, appImages } from './storage/resource';

defineBackend({
  auth,
  appThumbnails,
  appImages,
});

in your app code

import { list } from 'aws-amplify/storage';

useEffect(() => {
    const getImages = async () => {
      try {
        const result = await list({
            path: '',
            options: {
                bucket: 'appImages'
            }
        });
        setThumbnails(result);
        console.log(result);
      } catch (error) {
        console.log( error);
      }
    };

    getImages();
  }, []);

changed some names from https://docs.amplify.aws/react-native/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets

AWS
답변함 3달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠