2014-03-19 122 views
0

我可以將excel文件上傳到AWS s3帳戶嗎?我有什麼是庫中提供的PutObject方法可用於從一個位置上傳文件或使用Stream對象。我可以使用AWSSDK.dll將excel文件上傳到Amazon S3

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

       PutObjectResponse response = client.PutObject(request); 

鍵可以是機器上的絕對路徑或我們給文件的流。但我的疑問是,我們如何使用上述方法上傳Excel文件

P.S 這是我用來將流轉換爲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(); 
      } 
     } 

回答

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

非常感謝您的答覆。但我懷疑是應該分配給PutObjectRequest的ContentType屬性。另一個疑問是如何獲得excel文件的流。正如我GOOGLE了,我沒有找到任何直接的方式來獲得Excel文件的流 – Vikram

+0

@Vikram ContentType只是簡單的MIME類型,這通常很容易查找。我已經添加了如何讀取文件的示例,如果您正在從文件路徑中讀取文件,該文件應該可以正常工作。如果你有一些其他形式的Excel文件,其中一個流不是那麼簡單,我不知道這是什麼形式。 –

+0

所以基本上我有WCF服務,我將使用上傳文件到aws,但由於我們不能有一個文件流作爲參數服務,所以我有一個字節[],我發送到服務。請將P.S – Vikram