首页 > 解决方案 > 使用 VB.NET 上传 AWS S3 文件

问题描述

我正在尝试使用 AWS 提供的示例代码将文件从 VB.net Web 应用程序上传到 S3。我将 C# 示例代码转换为 VB,并将其包含在一个名为 UploadToS3.vb 的单独文件中。我遇到的问题是我不知道如何将文件传递到 UploadToS3.vb 文件并上传。

在 Web 应用程序的前端,我使用以下内容选择文件并提交:

    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblUpload" runat="server" Text="UPLOAD"></asp:Label><br />
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
</form>

文件后面的代码:

Namespace Amazon.DocSamples.S3
Class UploadFileMPUHighLevelAPITest
    Private Const bucketName As String = "webapp.company.com"
    Private Const keyName As String = "*** provide a name for the uploaded object ***"
    Private Const filePath As String = "*** provide the full path name of the file to upload ***"
    Private Shared ReadOnly bucketRegion As RegionEndpoint = RegionEndpoint.USEast1
    Private Shared s3Client As IAmazonS3

    Public Shared Sub Main()
        s3Client = New AmazonS3Client(bucketRegion)
        UploadFileAsync().Wait()
    End Sub

    Private Shared Async Function UploadFileAsync() As Task
        Try
            Dim fileTransferUtility = New TransferUtility(s3Client)
            Await fileTransferUtility.UploadAsync(filePath, bucketName)
            Console.WriteLine("Upload 1 completed")
            Await fileTransferUtility.UploadAsync(filePath, bucketName, keyName)
            Console.WriteLine("Upload 2 completed")

            Using fileToUpload = New FileStream(filePath, FileMode.Open, FileAccess.Read)
                Await fileTransferUtility.UploadAsync(fileToUpload, bucketName, keyName)
            End Using

            Console.WriteLine("Upload 3 completed")
        Catch e As AmazonS3Exception
            Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message)
        Catch e As Exception
            Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message)
        End Try
    End Function
End Class
End Namespace

AWS 的示例代码:https ://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html

更具体的问题: “文件路径”的值是什么样的?只是 S3 存储桶中文件夹的名称?如果默认使用上传文件的名称,是否需要 Private Const keyname as string...?我在提交按钮单击事件中放入什么来调用 S3 上传?

任何帮助将不胜感激。

标签: .netvb.netvisual-studioamazon-s3file-upload

解决方案


不,我认为文件路径是函数UploadAsync将获取文件以将其上传到 S3 的文件的路径。

您可以这样做的方法是,vb 应用程序需要在主机中的服务器中,当您上传时,我认为您需要将文件保存到服务器中的临时文件夹,然后使用 S3 功能UploadAsync将文件上传到 S3。


推荐阅读