ios 上的 AWSS3TransferUtilityErrorDomain 代码=2
AWSS3TransferUtilityErrorDomain Code=2 on ios
AWSS3TransferUtilityErrorDomain Code=2
在 iOS 上传达到 100% 时出现此错误,而 android 工作正常。
我正在使用 react-native-s3
。但这似乎是 sdk 或我的存储桶策略的问题,但我不知道如何解决这个问题。
更多信息:
要上传,我使用的是从我们的服务器生成的 CognitoCredentials、区域、IdentityPool 和 sessionToken。
编辑:
这只发生在 iOS。 Android 工作正常。
通常这是一个 S3 存储桶策略问题,权限。下面是 s3 的示例策略脚本。
<key>AWS</key>
<dict>
<key>CredentialsProvider</key>
<dict>
<key>CognitoIdentity</key>
<dict>
<key>Default</key>
<dict>
<key>PoolId</key>
<string>us-west-2:xxxxx</string>
<key>Region</key>
<string>USWest2</string>
</dict>
</dict>
</dict>
<key>S3TransferUtility</key>
<dict>
<key>Default</key>
<dict>
<key>Region</key>
<string>**USEast1**</string>
</dict>
</dict>
</dict>
所以看来我的问题是错误的区域。我不知道为什么 Android 有效。
我知道这是一个老问题,但是最好写下我的情况和解决方案。
在我的 iOS (Swift) 项目中,我尝试在登录后上传图片(使用 Cognito),但出现此错误:
Error Domain=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=2 "(null)"
下面的代码看起来一切正常:
transferUtility.uploadData(
data,
key: "my-picture.png",
contentType: "image/png",
expression: expression,
completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
if let error = task.error {
print("Error: \(error.localizedDescription)")
DispatchQueue.main.async {
print("Error on upload: \(error.localizedDescription)")
}
}
if let _ = task.result {
DispatchQueue.main.async {
print("Upload Starting!")
}
}
return nil;
}
解决方案在 AWS Amplify Storage documentation。如那里所述,我应该写 key: "private/{user_identity_id}/my-picture.png"
这样的键值
也许,以后对大家有帮助。
Check your ARN at the time of adding policy to S3 bucket. It should be
like
arn:aws:s3:::yourBucketName/*
if you are uploading in your bucket directly without any sub
directory.
就我而言,问题的原因是文件名中的特殊符号,特别是 %
。在上传文件之前过滤文件名中的特殊符号解决了这个问题。
在我的例子中,我配置了所有区域,但在为我设置工作后我忘记设置 headerRequest 和 parameterRequest
expression.setValue("public-read-write", forRequestHeader: "x-amz-acl")
expression.setValue("public-read-write", forRequestParameter: "x-amz-acl")
AWSS3TransferUtilityErrorDomain Code=2
在 iOS 上传达到 100% 时出现此错误,而 android 工作正常。
我正在使用 react-native-s3
。但这似乎是 sdk 或我的存储桶策略的问题,但我不知道如何解决这个问题。
更多信息: 要上传,我使用的是从我们的服务器生成的 CognitoCredentials、区域、IdentityPool 和 sessionToken。
编辑: 这只发生在 iOS。 Android 工作正常。
通常这是一个 S3 存储桶策略问题,权限。下面是 s3 的示例策略脚本。
<key>AWS</key>
<dict>
<key>CredentialsProvider</key>
<dict>
<key>CognitoIdentity</key>
<dict>
<key>Default</key>
<dict>
<key>PoolId</key>
<string>us-west-2:xxxxx</string>
<key>Region</key>
<string>USWest2</string>
</dict>
</dict>
</dict>
<key>S3TransferUtility</key>
<dict>
<key>Default</key>
<dict>
<key>Region</key>
<string>**USEast1**</string>
</dict>
</dict>
</dict>
所以看来我的问题是错误的区域。我不知道为什么 Android 有效。
我知道这是一个老问题,但是最好写下我的情况和解决方案。 在我的 iOS (Swift) 项目中,我尝试在登录后上传图片(使用 Cognito),但出现此错误:
Error Domain=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=2 "(null)"
下面的代码看起来一切正常:
transferUtility.uploadData(
data,
key: "my-picture.png",
contentType: "image/png",
expression: expression,
completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
if let error = task.error {
print("Error: \(error.localizedDescription)")
DispatchQueue.main.async {
print("Error on upload: \(error.localizedDescription)")
}
}
if let _ = task.result {
DispatchQueue.main.async {
print("Upload Starting!")
}
}
return nil;
}
解决方案在 AWS Amplify Storage documentation。如那里所述,我应该写 key: "private/{user_identity_id}/my-picture.png"
也许,以后对大家有帮助。
Check your ARN at the time of adding policy to S3 bucket. It should be like
arn:aws:s3:::yourBucketName/*
if you are uploading in your bucket directly without any sub directory.
就我而言,问题的原因是文件名中的特殊符号,特别是 %
。在上传文件之前过滤文件名中的特殊符号解决了这个问题。
在我的例子中,我配置了所有区域,但在为我设置工作后我忘记设置 headerRequest 和 parameterRequest
expression.setValue("public-read-write", forRequestHeader: "x-amz-acl") expression.setValue("public-read-write", forRequestParameter: "x-amz-acl")