Upload files to Amazon s3

1

Dear, I have a web project that I am developing with Microsoft technologies. The project itself has several types of projects, wcf, MVC, among others.

What I need to do is upload files (they can be images, documents and different types of documents in .doc, .xls, etc, of course making the respective size validations).

The issue is that I have read a lot of documentation about it, however, I would like to have an example of someone who has done this feat.

In particular, have the example of the connection configuration to Amazon S3 and an example in which a file is saved in that repository. I thank you in advance for your cooperation. Greetings community.

    
asked by Fernando Campos 17.03.2017 в 23:03
source

1 answer

2

Well, I have not done anything in .net or less in WCF related to AWS, but the basic thing is, first, to have obviously installed the AWS SDK for .Net, configure the access credentials to the bucket which is explained in link and use them later with the S3 client instance:

try {
    /// crear la solicitud para subir el archivo
    /// recibe 3 parámetros: 
    /// 1. BucketName: el nombre del bucket
    /// 2. Key: el nombre del archivo, es como se quiere llamar, incluso con ruta relativa. Ejemplo: directorio/archivo.txt
    /// 3. FilePath: ruta física del archivo a subir
    PutObjectRequest request = new PutObjectRequest()
    {
        BucketName = bucketName,
        Key = keyName,
        FilePath = filePath
    };
    PutObjectResponse response = client.PutObject(request); 
} 
catch (AmazonS3Exception amazonS3Exception)
{
    if (amazonS3Exception.ErrorCode != null &&
      (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
      ||
      amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
    {
        Console.WriteLine("Check the provided AWS Credentials.");
        Console.WriteLine("For service sign up go to http://aws.amazon.com/s3");
    }
    else
    {
        Console.WriteLine("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message);
    }
}

By the way, the size of the file does not matter much since you have a good connection, although obviously you should not have files of more than 50MB of Office (EMO) since opening them would be tortuous.

    
answered by 18.03.2017 в 02:32