.net - 使用 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 上传?
任何帮助将不胜感激。
解决方案
不,我认为文件路径是函数UploadAsync将获取文件以将其上传到 S3 的文件的路径。
您可以这样做的方法是,vb 应用程序需要在主机中的服务器中,当您上传时,我认为您需要将文件保存到服务器中的临时文件夹,然后使用 S3 功能UploadAsync将文件上传到 S3。
推荐阅读
- android - 应用程序可以在不转到 Wifi 设置的情况下将手机/智能手机连接到 Wifi 网络吗?
- javascript - Chome 浏览器 onbeforeunload 事件无法显示警告弹出窗口,但如果我们打开 chrome 开发工具,则可以正常工作
- powershell - 如何在 PowerShell 中用连续数字重命名每个 .jpg 文件名
- bash - 基于 C++ 二进制运行时参数创建 Bash 脚本文件名
- javascript - 如何在 MarkLogic 10 的资源扩展中导入 JavaScript 模块
- c# - 当 oject 不为空时,为什么我在搜索查询中收到 NullReferenceException
- reactjs - 如何使用 setState 回调函数进行条件函数调用
- regex - oracle regexp_like:使用 Perl 正则表达式与运算符等效
- c# - 有点像头文件,我可以在 C# 中分离一些定义吗?
- python - Pandas 突然无法打开 Excel 文件(在 OLE2 复合文档中找不到工作簿