从AWS S3文件URL下载ASP.net客户端

时间:2015-04-22 15:31:20

标签: asp.net vb.net amazon-web-services amazon-s3

首先让我说asp对我来说是一个新的冒险,我正在完成一个当前的项目。

基本上我正构建一个用户登录并获取与其访问权限相关联的文件的网站。我使用AWS S3来托管文件。我解决了一个解决方案是将文件下载到服务器,然后将它们提供给客户端。问题是用户必须等到文件下载到Web服务器然后它将提示用户打开/保存。对于小文件,这不是一个糟糕的解决方案,但有些文件可以超过700mb。

所以我想生成该文件的URL。我在下面做了这个。

    Public Function GenerateDownLoadURL(keyName As String) As String
    'You would generate a URL if you want to share a file but the AllUsers group does not have read permission to the object.
    'The following exampe creates a URL that is valid for 1 day.
    'Query String Authentication: http://docs.amazonwebservices.com/AmazonS3/latest/S3_QSAuth.html
    'Query String Request Authentication Alternative: http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationQueryStringAuth
    Dim useSSL As Boolean = True
    Dim appConfig As NameValueCollection = ConfigurationManager.AppSettings
    Dim endPoint As String = "s3.amazonaws.com"
    Dim bucket As String = "bucketName"
    Dim dil As String = "/"
    'Dim pre As String = Path
    Dim key As String = appConfig.Get("AWSAccessKey")
    Dim sec As String = appConfig.Get("AWSSecretKey")
    Dim MyS3Helper As New SprightlySoftAWS.S3.Helper

    Dim ExpiresURL As String
    'Build Url Expires in 30 min
    ExpiresURL = MyS3Helper.BuildExpiresURL(useSSL, endPoint, bucket, keyName, "", DateAdd(DateInterval.Minute, 30, Now), key, sec)

    Return ExpiresURL

End Function

网址生成效果很好,但我想要的是在新的客户端标签/窗口中导航到的网址。由于此URL将为用户启动下载。作为asp.net的新手,我不确定如何做到这一点。

我试过这样的事情,但这并不奇怪,这不起作用。

 Public Sub DownloadObject(ByVal keyName As String)
    Dim keySplit() As String = keyName.Split("/"c)
    Dim fileName As String = keySplit(keySplit.Length - 1)
    Dim dest As String = GenerateDownLoadURL(keyName)


    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" & fileName)
    HttpContext.Current.Response.ContentType = "application/octet-stream"
    HttpContext.Current.Response.TransmitFile(dest)
    HttpContext.Current.Response.Flush()
    HttpContext.Current.Response.End()


End Sub

感谢您的帮助!!!

0 个答案:

没有答案