Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When trying to upload an image from the website to the AWS bucket I get an ajax error. However, when I run the application from visual studio, it loads without problems.

The code C# I use in the controller is the following:

C#
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
    var s3Client = new AmazonS3Client(accesskey, secretkey, bucketRegion);
    var fileTransferUtility = new TransferUtility(s3Client);
    try
    {
        if (file.ContentLength > 0)
        {
            var filePath = Path.Combine(Server.MapPath("~/"), Path.GetFileName(file.FileName));
            var fileTransferUtilityRequest = new TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                FilePath = filePath,
                StorageClass = S3StorageClass.StandardInfrequentAccess,
                Key = keyName,
                CannedACL = S3CannedACL.PublicRead
            };
            fileTransferUtility.Upload(fileTransferUtilityRequest);
            fileTransferUtility.Dispose();
        }
        ViewBag.Message = "File Uploaded Successfully!!";
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        if (amazonS3Exception.ErrorCode != null &&
            (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
            ||
            amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
        {
            ViewBag.Message = "Check the provided AWS Credentials.";
        }
        else
        {
            ViewBag.Message = "Error occurred: " + amazonS3Exception.Message;
        }
    }
    return RedirectToAction("Index");
}


What I have tried:

I have tried other options that I found online with no results. The region is USEast2 and the code is as follows:

C#
IAmazonS3 client = new AmazonS3Client(accesskey, secretkey, bucketRegion);  

FileInfo localFile = new FileInfo(@"c:\test.txt");  
string destPath = @"folder\sub-folder\test.txt"; // <-- high-level s3 path uses \
 
S3FileInfo s3File = new S3FileInfo(client, "my-bucket-name", destPath);  
if (!s3File.Exists)  
{  
    using (var s3Stream = s3File.Create()) // <-- create file in S3  
    {  
        localFile.OpenRead().CopyTo(s3Stream); // <-- copy the content to S3  
    }  
}


Another option that I have tried is with the following code and the error is presented on the line
InputStream = localFile.OpenRead(),

There the error says that it cannot find the file path

C#
PutObjectRequest request = new PutObjectRequest()
{
    InputStream = localFile.OpenRead(),
    BucketName = bucketName,
    Key = path // <-- in S3 key represents a path
};

PutObjectResponse response = client.PutObject(request);


I bring the image with this code, where I capture the image in the upload input
C#
FileInfo localFile = new FileInfo(image.FileName);
Posted
Updated 1-Jun-24 4:04am
v2
Comments
Pete O'Hanlon 1-Jun-24 3:55am    
What error do you get back? Without knowing this, we can't really help.
Member 12760369 1-Jun-24 9:40am    
The error occurs when the system searches for the image in the path where the file is located. In the debugger I had to copy the image to be copied to the project folder.
Dave Kreskowiak 1-Jun-24 11:47am    
One more time. Without the exact error message that's coming back from AWS, it's impossible to help you accurately.
Member 12760369 1-Jun-24 9:49am    
It is worth mentioning that in the debugger the application runs at the address https://localhost:44310/ while in AWS it runs at the address provided by Amazon in the domain elasticbeanstalk.com

1 solution

I'm going to take a guess here and suggest that your keys are different in the deployed version of your application. If you stored them in your application.development.json file and they don't match what is deployed, this would account for the differences in behaviour.

Edit. As you have now added that the line that us failing is the local file read, we now have enough information to diagnose your issue. The reason the read is failing is because you are trying to read from an area outside your website. This is prevented because it would be a security violation to allow you to do this.
 
Share this answer
 
v2
Comments
Member 12760369 1-Jun-24 9:38am    
The keys are the same as the ones I use in the deployment, I have them configured in the web.config. The error occurs when the system searches for the image in the path where the file is located. In the debugger I had to copy the image to be copied to the project folder.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900