1

In my application, I need to take an input file, an image in this case, upload it an S3 bucket and return the URL, the address used for retrieval and store that in a database.

I correctly upload the image file in S3 and what I am not sure of is how to retrieve the URL for the S3 object.

the current code is

        public string WriteImageEx(byte[] image, string fileName)
    {
        var s3client = InitS3Client();

        try
        {
            var request = new PutObjectRequest
            {
                BucketName = bucketName,
                Key = subfolder + "/" + fileName,
                CannedACL = S3CannedACL.PublicRead,
            };

            logger.Debug("Attempting to write to s3 - BucketName [" + request.BucketName + "], Key [" + request.Key + "]");

            using (MemoryStream stream = new MemoryStream(image))
            {
                request.InputStream = stream;

                PutObjectResponse response = s3client.PutObject(request);


                logger.Debug("s3 PutObjectResponse:  HttpStatusCode[" + response.HttpStatusCode + "], ETag [" + response.ETag + "], Expiration [" + response.Expiration + "], VersionId [" + response.VersionId + "]");

                return bucketName + DELIMITER + request.Key;
            }
        }

        catch (Exception e)
        {
            logger.Error("WriteImageEx failed: ", e);
        }

        return null;
    }

Thoughts?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
reza
  • 5,972
  • 15
  • 84
  • 126

0 Answers0