【发布时间】:2020-05-09 04:57:33
【问题描述】:
我正在尝试使用 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 上传?
我们将不胜感激。
【问题讨论】:
标签: .net vb.net visual-studio amazon-s3 file-upload