lundi 21 avril 2014

c# - puis-je télécharger excel fichier dans Amazon S3 à l'aide de AWSSDK.dll - Stack Overflow


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();
}
}



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);
}


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();
}
}


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);
}

0 commentaires:

Enregistrer un commentaire