![](/img/trans.png)
[英]Getting Access Denied Exception when deleting a file in Amazon S3 using the .Net AWSSDK
[英]Can I upload excel file into Amazon S3 using AWSSDK.dll
我可以將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文件
PS這是我用來將流轉換為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();
}
}
您應該能夠通過文件路徑或流上傳任何文件。 沒關系,這是一個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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.