简体   繁体   中英

Can I upload excel file into Amazon S3 using AWSSDK.dll

Can I upload excel file into the AWS s3 account. What I have fount is that PutObject method provided in the Library can be used to upload the file from a location or using the Stream object.

PutObjectRequest request = new PutObjectRequest()
                {
                    ContentBody = "this is a test",
                    BucketName = bucketName,
                    Key = keyName,
                    InputStream = stream
                };

                PutObjectResponse response = client.PutObject(request);

Key can be the absolute path on the machine or we give the stream of the file. But my doubt is how we can upload the excel file using the above method

PS This is the way I am using to convert stream to byte[] but input.ReadByte() is always equal to zero. So my doubt is, is it not reading the excel file?

FileStream str = new FileStream(@"C:\case1.xlsx", FileMode.Open);            
byte[] arr = ReadFully(str);


public static byte[] ReadFully(FileStream input)
        {
            long size = 0;
            while (input.ReadByte() > 0)
            {
                size++;
            }
            byte[] buffer = new byte[size];
            //byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

You should be able to upload any file via the file path or stream. It doesn't matter that it's an Excel file. When you run PutObject , it uploads the actual file data represented by that path or stream.

You can see the MIME types for MS Office formats at Filext . Doing it by file path would probably be easier:

PutObjectRequest request = new PutObjectRequest()
{
    ContentBody = "this is a test",
    BucketName = bucketName,
    Key = keyName,
    ContentType =
 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // xlsx
    FilePath = @"\path\to\myfile.xlsx"
};

PutObjectResponse response = client.PutObject(request);

Or reading from a file stream:

PutObjectRequest request = new PutObjectRequest()
{
    ContentBody = "this is a test",
    BucketName = bucketName,
    Key = keyName,
    ContentType =
 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" // xlsx
};
using (var stream = new FileStream(@"\path\to\myfile.xlsx", FileMode.Open))
{
    request.InputStream = stream;

    PutObjectResponse response = client.PutObject(request);
}

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