2014-03-19 130 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