我可以将excel文件上传到AWS s3帐户吗?我所拥有的是,库中提供的PutObject方法可用于从某个位置或使用Stream对象上载文件。
PutObjectRequest request = new PutObjectRequest()
{
ContentBody = "this is a test",
BucketName = bucketName,
Key = keyName,
InputStream = stream
};
PutObjectResponse response = client.PutObject(request);
键可以是机器上的绝对路径,也可以是文件流。但我怀疑的是我们如何使用上述方法上传excel文件
P.S 这是我用来将stream转换为byte []的方法,但input.ReadByte()总是等于零。所以我怀疑,是不是在阅读excel文件?
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();
}
}
答案 0 :(得分:1)
您应该能够通过文件路径或流上传任何文件。它不是一个Excel文件并不重要。当您运行PutObject
时,它会上传该路径或流所代表的实际文件数据。
您可以在Filext查看MS Office格式的MIME类型。按文件路径执行操作可能会更容易:
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);
或者从文件流中读取:
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);
}