如何在执行 S3 操作时停止 aws-amplify 在 url 中附加 level 和 identityPoolId
How to stop aws-amplify from appending level and identityPoolId in the url while performing S3 operations
我按照 aws-amplify docs and medium post 中的文章设置了托管在 S3 上的 React js 网站,并使用 Cognito 进行身份验证。一切都按记录工作,但我在自定义 URLs 以在 S3 存储桶(不是用于托管我的网站的存储桶)上执行操作时遇到困难
我的 S3 存储桶中的对象不在级别(public、私有、受保护)下,也不在 identityPoolId 密钥下。
例如,我的存储桶中的典型对象键如下所示
project/run/ad33dff21f3g53/result.txt
但是,下面是我的应用程序中的示例代码
componentDidMount() {
Amplify.configure({
Auth: {
identityPoolId: awsconfig.aws_cognito_identity_pool_id,
region: awsconfig.aws_cognito_region,
userPoolId: awsconfig.aws_user_pools_id,
userPoolWebClientId: awsconfig.aws_user_pools_web_client_id
},
Storage: {
bucket: awsconfig.my_results_bucket,
region: awsconfig.aws_cognito_region,
identityPoolId: awsconfig.aws_cognito_identity_pool_id
}
});
SetS3Config(awsconfig.my_results_bucket, "private");
Storage.get("project/run/ad33dff21f3g53/result.txt", {
download: true,
level: "private"
})
.then(result => {
console.log(result.Body.toString());
this.setState({ content: result.Body.toString() });
})
.catch(err => console.log(err));
}
export function SetS3Config(bucket, level) {
Storage.configure({
bucket: bucket,
level: level,
region: awsconfig.aws_cognito_region,
identityPoolId: awsconfig.aws_cognito_identity_pool_id
});
}
不起作用,正如我在浏览器的网络选项卡中看到的,对 s3 存储桶 API 的请求按以下模式命中
https://my_results_bucket.s3.eu-west-2.amazonaws.com/private/eu-west-2%3A0e197abd3-004e-4af8-ba56-32ff0d534f67/project/run/ad33dff21f3g53/result.txt
这会引发 404 错误,因为我的对象不在指定的键下。
有什么方法可以阻止 aws amplify 将 level
和 identityPoolId
添加到 URL 中?
PS:
如果我将我的对象放在上面 URL 中所述的确切位置,它就会起作用。但是,我不能这样做,因为我的 S3 存储桶中的密钥有很多依赖项。
据我了解,这恐怕无法与 aws-amplify 一起使用。
目前,您最多可以实现的是为每个级别设置 customPrefixes:
const customPrefix = {
public: 'myPublicPrefix/',
protected: 'myProtectedPrefix/',
private: 'myPrivatePrefix/'
};
Storage.put('test.txt', 'Hello', {
customPrefix: customPrefix,
level: \ one of private, protected or public
})
.then (result => console.log(result))
.catch(err => console.log(err));
我已经恢复到使用 aws-sdk 来执行复杂的操作:
const credentials = await Auth.currentCredentials();
const essentials = await Auth.essentialCredentials(credentials)
console.log(essentials)
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
region: YOUR_REGION,
credentials:Auth.essentialCredentials(credentials)
});
var params = {Bucket: YOUR_BUCKET , Key: YOUR_OBJECT_PATH};
var url = s3.getSignedUrl('getObject', params);
console.log('The URL is', url);
这需要将策略附加到您的用户角色以授予他执行所需操作的适当权限,因此前往 IAM 并创建一个策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::YOUR_BUCKET/YOUR_OBJECT_PATH"
],
"Effect": "Allow"
}
]
}
将此策略附加到需要访问权限的 auth/unauth 角色,您就可以开始了。希望我能帮到你
我按照 aws-amplify docs and medium post 中的文章设置了托管在 S3 上的 React js 网站,并使用 Cognito 进行身份验证。一切都按记录工作,但我在自定义 URLs 以在 S3 存储桶(不是用于托管我的网站的存储桶)上执行操作时遇到困难
我的 S3 存储桶中的对象不在级别(public、私有、受保护)下,也不在 identityPoolId 密钥下。 例如,我的存储桶中的典型对象键如下所示
project/run/ad33dff21f3g53/result.txt
但是,下面是我的应用程序中的示例代码
componentDidMount() {
Amplify.configure({
Auth: {
identityPoolId: awsconfig.aws_cognito_identity_pool_id,
region: awsconfig.aws_cognito_region,
userPoolId: awsconfig.aws_user_pools_id,
userPoolWebClientId: awsconfig.aws_user_pools_web_client_id
},
Storage: {
bucket: awsconfig.my_results_bucket,
region: awsconfig.aws_cognito_region,
identityPoolId: awsconfig.aws_cognito_identity_pool_id
}
});
SetS3Config(awsconfig.my_results_bucket, "private");
Storage.get("project/run/ad33dff21f3g53/result.txt", {
download: true,
level: "private"
})
.then(result => {
console.log(result.Body.toString());
this.setState({ content: result.Body.toString() });
})
.catch(err => console.log(err));
}
export function SetS3Config(bucket, level) {
Storage.configure({
bucket: bucket,
level: level,
region: awsconfig.aws_cognito_region,
identityPoolId: awsconfig.aws_cognito_identity_pool_id
});
}
不起作用,正如我在浏览器的网络选项卡中看到的,对 s3 存储桶 API 的请求按以下模式命中
https://my_results_bucket.s3.eu-west-2.amazonaws.com/private/eu-west-2%3A0e197abd3-004e-4af8-ba56-32ff0d534f67/project/run/ad33dff21f3g53/result.txt
这会引发 404 错误,因为我的对象不在指定的键下。
有什么方法可以阻止 aws amplify 将 level
和 identityPoolId
添加到 URL 中?
PS: 如果我将我的对象放在上面 URL 中所述的确切位置,它就会起作用。但是,我不能这样做,因为我的 S3 存储桶中的密钥有很多依赖项。
据我了解,这恐怕无法与 aws-amplify 一起使用。 目前,您最多可以实现的是为每个级别设置 customPrefixes:
const customPrefix = {
public: 'myPublicPrefix/',
protected: 'myProtectedPrefix/',
private: 'myPrivatePrefix/'
};
Storage.put('test.txt', 'Hello', {
customPrefix: customPrefix,
level: \ one of private, protected or public
})
.then (result => console.log(result))
.catch(err => console.log(err));
我已经恢复到使用 aws-sdk 来执行复杂的操作:
const credentials = await Auth.currentCredentials();
const essentials = await Auth.essentialCredentials(credentials)
console.log(essentials)
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
region: YOUR_REGION,
credentials:Auth.essentialCredentials(credentials)
});
var params = {Bucket: YOUR_BUCKET , Key: YOUR_OBJECT_PATH};
var url = s3.getSignedUrl('getObject', params);
console.log('The URL is', url);
这需要将策略附加到您的用户角色以授予他执行所需操作的适当权限,因此前往 IAM 并创建一个策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::YOUR_BUCKET/YOUR_OBJECT_PATH"
],
"Effect": "Allow"
}
]
}
将此策略附加到需要访问权限的 auth/unauth 角色,您就可以开始了。希望我能帮到你