简体   繁体   中英

Upload file into S3 with AWS SDK ASP.NET

I am trying to upload an image from ASP.NET to S3. I am using AWS SDK for that and have already set up what is needed. However, after i run my project, i received an error. I'll be replacing my bucket name to ... for this sample code.

I set up my secretkey and accesskey from User in my Web.config. Please do tell me if u need more codes. I need help.

controller

private static readonly string _awsAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];

private static readonly string _awsSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];

[HttpPost]  
        public ActionResult UploadFile(HttpPostedFileBase file)  
        {
            try  
            {
                if (file.ContentLength > 0)
                {

                    IAmazonS3 client;
                    using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey))
                    {
                        PutObjectRequest request = new PutObjectRequest
                        {
                            BucketName = "...",
                            CannedACL = S3CannedACL.PublicRead,
                            Key = "images/" + (DateTime.Now.ToBinary() + "-" + file.FileName),
                            FilePath = Server.MapPath("~/UploadedFiles")
                        };

                        client.PutObject(request);
                    }


                }

                imageUrls = "File Uploaded Successfully!!";
                System.Diagnostics.Debug.WriteLine("File Uploaded Successfully!!");

                return Json(imageUrls);
            }  
            catch  
            {  
                ViewBag.Message = "File upload failed!!";
                System.Diagnostics.Debug.WriteLine("File upload failed!!");
                return Json(ViewBag.Message);  
            }  
        }

You're getting the error due to DateTime.Now.ToBinary() which contains invalid characters to be used in a URL. For example, you could use a GUID or a Unix timestamp instead.

Also, the FilePath property you're assigning to the PutObjectRequest is the full path and name to a file to be uploaded. So, you don't need it when you already have HttpPostedFileBase as an input parameter, which contains the InputStream property (ie, the stream object).

Your PutObjectRequest should look something like this:

.
.
.
Guid guid = Guid.NewGuid();

// Create a client
AmazonS3Client client = new AmazonS3Client(_awsAccessKey, _awsSecretKey);

// Create a PutObject request
PutObjectRequest request = new PutObjectRequest
{
    BucketName = "...",
    CannedACL = S3CannedACL.PublicRead,
    Key = "images/" + guid + "-" + file.FileName
};

using (System.IO.Stream inputStream = file.InputStream)
{
    request.InputStream = inputStream;

    // Put object
    PutObjectResponse response = client.PutObject(request);
}
.
.
.

I finally solved it. I realized i did not place region in AWSClientFactory, right at the end after the keys.

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