简体   繁体   中英

Uploading zip file to S3 from c#

So Im trying to upload a zip file to s3 for storage. But I keep getting 403 forbidden back. My code works when i upload an image file but not when i upload a zip file

My code:

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();
        }
    }

Can anyone see what the problem here is? i get a 403 forbidden in the s3Exception. the credentials im using does have write permission and works perfectly when i use a base64 image and change the contentType to "image/jpeg"

OK SO I FOUND THE FIX....

instead of using

CognitoAWSCredentials credentials = new CognitoAWSCredentials("###PVTCredentials###", Amazon.RegionEndpoint.EUWest1);

            client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.EUWest1);

i replaced it with

var client = new AmazonS3Client(AwsAccessKeyId,AwsSecretAccessKey, Amazon.RegionEndpoint.EUWest1);

For if anyone else is having this issue, replace CognitoAWSCredentials with id and secret credentials

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM