2011-04-19 128 views
1

我讀Amazon S3的文件,但我有點失落......直接寫入到Amazon S3

實際上,用戶上傳文件到Amazon S3,然後,我把它複製到EC2,proccess它,回S3和用戶可以下載它。

當我處理文件時,我需要創建一個html文件,並且需要在Amazon S3中創建,但我不知道如何直接在S3上創建此文件。現在,我在EC2上創建一個文件,然後將其移至S3。

要創建這個網站y使用:

  TextWriter tw = new StreamWriter(pathFile); 
      tw.WriteLine(page); 
      tw.Close(); 

是用HTML代碼的字符串,並pathFile是:string outputWap = Server.MapPath("~/xxx.html");

我使用ASP.NET MVC 2

感謝您的幫助。

問候。

回答

1

如果您正在使用適用於.Net的AWS開發工具包,您可以嘗試使用PutObjectRequestInputStream集合,而不是FilePath,儘管我自己並沒有嘗試過。

否則我真的不知道你現在正在做什麼的方式有什麼問題。

+0

謝謝你的回答傑夫,我要去嘗試它,我會留下一個反饋。 我很高興知道這是錯誤的。 PD:我忘記了,是的,我正在使用適用於.NET的AWS開發工具包:) – Pipeline 2011-04-19 14:23:12

+0

最後我在EC2上編寫html文件並將其複製到S3。與其他文件相同的方式。 感謝您的幫助:) – Pipeline 2011-04-20 10:29:58

+0

@Pipeline - 沒問題:) – 2011-04-20 11:06:25

2

此時,您不能在s3中創建(不要與上傳哪個當然是完全可行的)一個文件(至少在.net SDK中)。如果您無法控制本地文件系統(空間限制,權限或其他),則可以在內存中創建一個文件,然後將該文件上傳到s3,就好像它是本地存儲在磁盤上的文件一樣。代碼爲C#將如下:

public static bool CreateFile() 
    { 
     // Create file in memory 
     UnicodeEncoding uniEncoding = new UnicodeEncoding(); 

     // Create the data to write to the stream. 
     byte[] memstring = uniEncoding.GetBytes("you're file content here"); 
     using (MemoryStream memStream = new MemoryStream(100)) 
     { 
      memStream.Write(memstring, 0, memstring.Length); 

      // upload to s3 
      try 
      { 
       AmazonS3Client s3 = new AmazonS3Client(RegionEndpoint.USEast1); 
       using (Amazon.S3.Transfer.TransferUtility tranUtility = 
           new Amazon.S3.Transfer.TransferUtility(s3)) 
       { 
        tranUtility.Upload(memStream, bucketname, keyname); 

        return true; 
       } 
      } 
      catch (Exception ex) 
      { 
       return false; 
      } 
     } 

    }