简体   繁体   English

如何直接从Amazon s3存储中提供非公共文件

[英]How to serve non public file directly from the Amazon s3 storage

We need to work on .net based web application that will upload files to Amazon S3 Storage bucket using admin panel of the app and clients will be given to downloadable files with client.aspx file. 我们需要在基于.net的Web应用程序上工作,该应用程序将使用该应用程序的管理面板将文件上传到Amazon S3存储桶,并且将使用client.aspx文件为客户端提供可下载文件。

We looked at few example and got confused with some of the sample code for downloading non public files from S3 storage. 我们只看了几个示例,并对从S3存储下载非公共文件的一些示例代码感到困惑。 one such example is below 下面是一个这样的例子

AmazonS3Config config = new AmazonS3Config()
{
RegionEndpoint = RegionEndpoint.USEast1
};
IAmazonS3 client = new AmazonS3Client(accessKey, secretKey, config);
string dest = System.IO.Path.GetTempPath() + "event.mp4";
using (client)
{
GetObjectRequest request = new GetObjectRequest() { BucketName = "bucketname" + @"/" + "videos2015", Key = "event.mp4" };
using (GetObjectResponse response = client.GetObject(request))
{
response.WriteResponseStreamToFile(dest);
}
}
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + "dynamic_filename.png");
Response.ContentType = "application/octet-stream";
Response.TransmitFile(dest);
Response.Flush();
Response.End();

When user click on the link following code gets executed on web server and code downloads file on the web server and then serves the same file to client... if i am not wrong. 当用户单击链接时,以下代码在Web服务器上执行,代码下载Web服务器上的文件,然后将相同的文件提供给客户端...如果我没有记错的话。 Is there not a way that we can serve file for download directly from the AWS S3 storage. 有没有办法可以提供文件以直接从AWS S3存储中下载文件。

In above case it is waste of server resources and increases the download time also. 在上述情况下,这会浪费服务器资源,还会增加下载时间。

Out files on AWS are not Public they are non public so the url is not accessible directly from client browsers as is in case of public content type AWS上的输出文件不是公共文件,而是非公共文件,因此无法像公共内容类型一样直接从客户端浏览器访问该URL

The pre-signed urls are indeed what you are looking for. 预签名的URL确实是您想要的。 Since you are using C#, here is a link to some useful code examples: 由于您使用的是C#,因此以下是一些有用的代码示例的链接:

http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLDotNetSDK.html http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLDotNetSDK.html

There is no need to upload files to s3 thru your webserver, they can be sent directly. 无需通过网络服务器将文件上传到s3,它们可以直接发送。 Same thing on the download, download directly from S3 - don't copy them to EC2 first, you would be wasting bandwidth and processing resources. 下载时也是如此,直接从S3下载-不要先将它们复制到EC2,那样会浪费带宽和处理资源。

You can use Minio-dotnet client library Its Open Source & supports compatible S3 API. 您可以使用Minio-dotnet客户端库开放源代码并支持兼容的S3 API。

Here is an example for PresignedPostPolicy 这是PresignedPostPolicy的示例

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Minio;

namespace Minio.Examples
{
    class PresignedPostPolicy
    {
        static int Main()
        {
          /// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and
          /// my-objectname are dummy values, please replace them with original values.
            var client = new MinioClient("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");
            PostPolicy form = new PostPolicy();
            DateTime expiration = DateTime.UtcNow;
            form.SetExpires(expiration.AddDays(10));
            form.SetKey("my-objectname");
            form.SetBucket("my-bucketname");

            Dictionary <string, string> formData = client.PresignedPostPolicy(form);
            string curlCommand = "curl ";
            foreach (KeyValuePair<string, string> pair in formData)
            {
                    curlCommand = curlCommand + " -F " + pair.Key + "=" + pair.Value;
            }
            curlCommand = curlCommand + " -F file=@/etc/bashrc https://s3.amazonaws.com/my-bucketname";
            Console.Out.WriteLine(curlCommand);
            return 0;
        }
    }
}

And below one for PresignedPutObject 下方为PresignedPutObject

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Minio;

namespace Minio.Examples
{
    class PresignedPutObject
    {
        static int Main()
        {
          /// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and
          /// my-objectname are dummy values, please replace them with original values.
            var client = new MinioClient("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");
            Console.Out.WriteLine(client.PresignedPutObject("my-bucketname", "my-objectname", 1000));
            return 0;
        }
    }
}

Hope it helps. 希望能帮助到你。

PS: I work for Minio PS:我为Minio工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM