所以我正在尝试将 zip 文件上传到 s3 进行存储。但我一直收到 403 禁止返回。 我的代码在我上传图像文件时有效,但在我上传 zip 文件时无效
我的代码:
internal static void UploadFiletoS3fromZip(Byte[] fileByteArray, string fileName, string bucketName, string filepath)
{
try
{
CognitoAWSCredentials credentials = new CognitoAWSCredentials("###PVTCredentials###", Amazon.RegionEndpoint.EUWest1);
client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.EUWest1);
using (MemoryStream fileToUpload = new MemoryStream(fileByteArray))
{
PutObjectRequest request = new PutObjectRequest()
{
BucketName = bucketName,
Key = fileName,
InputStream = fileToUpload,
ContentType = "application/zip"
};
request.Timeout = TimeSpan.FromSeconds(60);
PutObjectResponse response2 = client.PutObject(request);
}
}
catch (AmazonS3Exception s3Exception)
{
s3Exception.ToExceptionless().Submit();
}
catch (Exception ex)
{
ex.ToExceptionless().Submit();
}
}
谁能看出这里的问题是什么?我在 s3Exception 中收到 403 禁止。当我使用 base64 图像并将 contentType 更改为“image/jpeg”时,我使用的凭据确实具有写入权限并且可以完美运行
好的,所以我找到了解决方法....
而不是使用
CognitoAWSCredentials credentials = new CognitoAWSCredentials("###PVTCredentials###", Amazon.RegionEndpoint.EUWest1);
client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.EUWest1);
我把它换成了
var client = new AmazonS3Client(AwsAccessKeyId,AwsSecretAccessKey, Amazon.RegionEndpoint.EUWest1);
如果其他人遇到此问题,请将 CognitoAWSCredentials 替换为 ID 和 secret 凭证
最佳答案
using (var client = new AmazonS3Client(LlaveAcceso, LlaveAccesoSecreta, RegionEndpoint.USEast2))
{
using (var newMemoryStream = new MemoryStream())
{
var putArchivo = new PutObjectRequest
{
BucketName = Buquet,
Key = file.FileName,
FilePath = ruta,
};
PutObjectResponse response = client.PutObjectAsync(putArchivo).Result;
MessageBox.Show("Archivo " + file.FileName + " Cargado Correctamente.", "AWS Loader", MessageBoxButtons.OK, MessageBoxIcon.Information);
label2.Text = "";
}
}
关于c# - 从 C# 上传 zip 文件到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51652856/