簡體   English   中英

AWS .NET SDK直接上傳到S3-預簽名URL

[英]AWS .NET SDK Direct Upload to S3 - Pre Signed URL

我想了解是否是將文件上傳到S3的特定.NET SDK示例( 在此處看到 )是首先上傳到我的服務器,還是直接上傳到S3?

我懷疑文件先上傳到我的服務器,然后保存到S3。 但是,也許SDK神奇地繞過了我的服務器? 這是代碼:

using System;
using System.IO;
using System.Net;
using Amazon.S3;
using Amazon.S3.Model;

namespace s3.amazon.com.docsamples
{
    class UploadObjcetUsingPresignedURL
    {
        static IAmazonS3 s3Client;
        // File to upload.
        static string filePath   = "*** Specify file to upload ***";
        // Information to generate pre-signed object URL.
        static string bucketName = "*** Provide bucket name ***";
        static string objectKey  = "*** Provide object key for the new object ***";

        public static void Main(string[] args)
        {
            try
            {
                using (s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
                {
                    string url = GeneratePreSignedURL();
                    UploadObject(url);

                }
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                    ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine(
                    "To sign up for service, go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine(
                     "Error occurred. Message:'{0}' when listing objects",
                     amazonS3Exception.Message);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }


        static void UploadObject(string url)
        {
            HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
            httpRequest.Method = "PUT";
            using (Stream dataStream = httpRequest.GetRequestStream())
            {
                byte[] buffer = new byte[8000];
                using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    int bytesRead = 0;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        dataStream.Write(buffer, 0, bytesRead);
                    }
                }
            }

            HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
        }

        static string GeneratePreSignedURL()
        {
            GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = bucketName,
                    Key        = objectKey,
                    Verb       = HttpVerb.PUT,
                    Expires    = DateTime.Now.AddMinutes(5)
                };

            string url = null;
            url = s3Client.GetPreSignedURL(request);
            return url;
        }
    }
}

再次,問題是
上面的示例是否首先上傳到服務器,然后將文件傳輸到S3? 還是以某種方式繞過服務器並直接上傳到S3?

謝謝!

編輯
正如TJ所說,答案是肯定的,它將首先通過我的服務器到達亞馬遜。 Kinda現在感覺像是一個愚蠢的問題。

如果有人感興趣,我決定使用帶有預簽名URL的CORS AJAX上傳到S3。 起初相當復雜,但現在可以正常工作了。 因此,我的服務器可以坐下來休息,同時可以進行無數的上傳!

這是這行:

while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)

在將字節寫入輸出流之前,它們已讀入服務器(不是持久存儲在磁盤上,而是保留在內存中)。 因此,您的服務器不會被繞過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM