mercredi 22 avril 2015

ASP.net Client Download from AWS S3 File URL

For first let me say that asp is a new venture for me and I am finishing up a current project.

So essentially I am building a website that users log in to and get files that are associated with their access privileges. I am used AWS S3 to host the files. One solution that i have worked out is downloading the file to the server and then offering them to the client. The problem is that the user has to wait till the file downloads to the web server and then it will prompt the user to open/save. For small files this is not a bad solution, but there are files that can be 700mb plus.

So I want to generate a URL to that file. I have done this below.

    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://ift.tt/1DNGABY
    'Query String Request Authentication Alternative: http://ift.tt/1buZch2
    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

The URL Generation works great, but what I want is for the URL to be navigated to in a new client tab/window. As this URL will start the download for the user. And being new to asp.net I'm not really sure how to do this.

I have attempted this like such, but not surprising this does not work.

 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

Thank you for your help!!!




Aucun commentaire:

Enregistrer un commentaire