从C#将zip文件上传到S3

时间:2018-08-02 11:47:18

标签: c# amazon-s3

因此,我试图将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);

对于其他任何人,如果遇到此问题,请使用id和秘密凭据替换CognitoAWSCredentials

1 个答案:

答案 0 :(得分:0)

        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 = "";
            }
        }