gpt4 book ai didi

javascript - 区域缺失错误 - AWS Amplify React

转载 作者:行者123 更新时间:2023-12-04 14:58:41 25 4
gpt4 key购买 nike

我正在尝试使用带有 React js 的放大功能将文件上传到 AWS,但我得到了 07:55.748 AWSS3Provider - error uploading Error: Region is missing .
这是我正在使用的代码。我什至尝试对该区域进行努力编码,但它仍然无法正常工作。

import { v4 as uuid } from 'uuid';
import { withAuthenticator } from '@aws-amplify/ui-react';
import { createDrawing as CreateDrawing } from '../graphql/mutations';
import { listDrawings as ListDrawings } from '../graphql/queries';
import config from '../aws-exports';
const { aws_user_files_s3_bucket: bucket } = config;

export const listDrawings = async () => {
const images = await API.graphql(graphqlOperation(ListDrawings));

console.log(images.data.listDrawings.items);
return images;
};

export const createDrawing = async (file) => {
const region = 'eu-west-1';
const extension = file.name.split('.')[1];
const imgName = file.name.split('.')[0];
if (file) {
const { type: mimeType } = file;
const key = `images/${uuid()}${imgName}.${extension}`;
const url = `https://${bucket}.s3.${region}.amazonaws.com/public/${key}`;
const inputData = { name: imgName, image: url };

try {
await Storage.put(key, file, {
contentType: mimeType
});
await API.graphql(
graphqlOperation(CreateDrawing, { input: inputData })
);
} catch (err) {
console.log('error: ', err);
}
}
};
我也试过更新 aws-amplify但它并没有再次解决我的问题。

最佳答案

在您导出文件的存储库中,有一个名为 aws-exports.js 的文件
它应该具有如下结构:

/* eslint-disable */
// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
"aws_project_region": "region",
"aws_cognito_identity_pool_id": "xxxxxxxxxxxxxxxx",
"aws_cognito_region": "region",
"aws_user_pools_id": "xxxxxxxxxxxxxxxx",
"aws_user_pools_web_client_id": "xxxxxxxxxxxxxxx",
"oauth": {xxxxxxxx}
};


export default awsmobile;
在区域中,编辑要添加的区域,例如 eu-west-1。
您也可以尝试像这样在文件中手动添加配置,而不是从 aws-exports.js 导入:
import Amplify, { Auth, Storage } from 'aws-amplify';

Amplify.configure({
Auth: {
identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab', //REQUIRED - Amazon Cognito Identity Pool ID
region: 'XX-XXXX-X', // REQUIRED - Amazon Cognito Region
userPoolId: 'XX-XXXX-X_abcd1234', //OPTIONAL - Amazon Cognito User Pool ID
userPoolWebClientId: 'XX-XXXX-X_abcd1234', //OPTIONAL - Amazon Cognito Web Client ID
},
Storage: {
AWSS3: {
bucket: '', //REQUIRED - Amazon S3 bucket name
region: 'XX-XXXX-X', //OPTIONAL - Amazon service region
}
}
});

关于javascript - 区域缺失错误 - AWS Amplify React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67372329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com