0

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

P.S 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();
            }
        }
Tim S.
  • 55,448
  • 7
  • 96
  • 122
Vikram
  • 1,617
  • 5
  • 17
  • 37

1 Answers1

1

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);
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • Thanks a lot for the reply. But my doubt is what should assign to the ContentType property of the PutObjectRequest. Another doubt is how can I get the stream of the excel file. As I googled, I didn't find any straight forward way to get the stream of the Excel file – Vikram Mar 19 '14 at 13:25
  • @Vikram the ContentType is simply the MIME type, which is usually easy to look up. I've added examples for how to read the file, which should work if you're reading it from a file path. If you have the Excel file in some other form where a stream is not so straightforward, I don't know what that form is. – Tim S. Mar 19 '14 at 13:33
  • So basically I have WCF service which i will be using to upload the file to aws, but as we cannot have a stream of file as parameter to service, so I am having a Byte[] which I am sending to the service. Please cehck my question with P.S – Vikram Mar 19 '14 at 13:49
  • As @TimS. stated, you can store and read Excel files with S3. Your problem seems to be that ReadFully() has a bug. It reads all the bytes from the input stream once to get the size, but then reads again to store the actual bytes. You don't have any bytes left because you've already read them! Easily fixed, but there are 3rd party libraries that will do it for you: http://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java – Bampfer Dec 12 '14 at 23:59