- 在AWS控制台中创建多个S3桶,并将每个S3桶的访问权限设置为“公有读取”。
- 在Amplify项目的“amplify/backend/storage”文件夹下创建多个S3存储配置,每个配置对应一个S3存储桶。配置方式如下:
{
"AWSS3": {
"bucketName": "bucketName1",
"region": "us-east-1",
"acl": "public-read",
"awsConfig":{
"accessKeyId": "",
"secretAccessKey": "",
"region": "us-east-1"
}
}
}
- 在Amplify项目中使用“withStorage”的高阶组件来访问S3存储桶。使用方式如下:
import { withStorage } from 'aws-amplify-react-native';
const MyComponent = ({ Storage }) => {
// 使用Storage对象访问S3存储桶
Storage.list('path/')
.then(result => console.log(result))
.catch(err => console.log(err));
return My Component;
};
export default withStorage(MyComponent);
- 在组件中使用不同的S3存储桶时,需要通过组件属性传递相应的存储配置。使用方式如下:
import { withStorage } from 'aws-amplify-react-native';
const MyComponent = ({ Storage, config }) => {
// 使用传递的config对象访问相应的S3存储桶
Storage.configure({ ...config, level: 'public' });
Storage.list('path/')
.then(result => console.log(result))
.catch(err => console.log(err));
return My Component;
};
export default withStorage(MyComponent);