将文件上传到 Amazon S3 存储的示例 vb.net 代码

Lei*_*dle 1 vb.net upload file-upload file amazon-s3

我正在寻找将文件上传到 Amazon S3 存储的示例 vb.net 项目。

我很乐意使用任何官方亚马逊 API,但不想使用 3rd 方产品。

问候, 利

小智 5

聚会有点晚了,但这是我用来处理 Amazon S3 存储的类。它将处理您试图完成的与 S3 上的文件有关的任何事情。

Imports Amazon.S3
Imports Amazon.S3.Model
Imports Amazon.Runtime
Imports Amazon
Imports Amazon.S3.Util
Imports System.Collections.ObjectModel
Imports System.IO

Public Class AWS_S3

    Const AWS_ACCESS_KEY As String = <your access key>
    Const AWS_SECRET_KEY As String = <your secret access key>


Private Property s3Client As IAmazonS3
'Usage for This Class
'
'       CreateABucket("<bucketname>") returns: empty string if successful/ error message if failed
'         Bucket name should conform with DNS requirements: 
'         - Should not contain uppercase characters
'         - Should not contain underscores (_)
'         - Should be between 3 and 63 characters long
'         - Should not end with a dash
'         - Cannot contain two, adjacent periods
'         - Cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid)
'       CreateFolder("<bucketname>", "<foldername>")  returns: empty string if successful/ error message if failed
'         Follow same rules as above
'       AddFileToFolder("<filename>", "<bucketname>", "<foldername>") returns: empty string if successful/ error message if failed
'       ListingFiles("<bucketname>", "<foldername>") return observable collection(of String) - empty collection if errors
'       DeleteFileFromFolder("<bucketname>", "<foldername>, <filename>) returns: empty string if successful/ error message if failed
'       GetFileFromFolder("<bucketname>", "<foldername>", <filename>, <target>) returns: empty string if successful/ error message if failed

Sub New()
    Try
        s3Client = New AmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY, RegionEndpoint.USWest2)
    Catch ex As Exception

    End Try

End Sub
'''<summary>
'''       CreateABucket("<bucketname>") returns: empty string if successful/ error message if failed
'''         Bucket name should conform with DNS requirements: 
'''         - Should not contain uppercase characters
'''         - Should not contain underscores (_)
'''         - Should be between 3 and 63 characters long
'''         - Should not end with a dash
'''         - Cannot contain two, adjacent periods
'''         - Cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid)
''' </summary>
    Public Function CreateABucket(bucketName As String) As String  'parameter : client As IAmazonS3
    Dim returnval As String = ""
    Try
        Try
            Dim putRequest1 As PutBucketRequest = New PutBucketRequest() With {.BucketName = bucketName, .UseClientRegion = True}
            Dim response1 As PutBucketResponse = s3Client.PutBucket(putRequest1)
        Catch amazonS3Exception As AmazonS3Exception
            If amazonS3Exception.ErrorCode = "BucketAlreadyOwnedByYou" Then
                returnval = "Bucket already exists"
            Else
                If (Not IsNothing(amazonS3Exception.ErrorCode) And amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")) Or amazonS3Exception.ErrorCode.Equals("InvalidSecurity") Then
                    returnval = "Check the provided AWS Credentials."
                Else
                    returnval = String.Format("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message)
                End If
            End If
        End Try
    Catch ex As Exception
        returnval = ""
    End Try
    Return returnval
  End Function
    Public Function CreateFolder(bucketName As String, folderName() As String) As String
    Dim returnval As String = ""
    Try
        Try
            Dim folderKey As String = ""
            If Not AmazonS3Util.DoesS3BucketExist(s3Client, bucketName) Then
                returnval = "Bucket does not exist"
            Else
                For i = 0 To folderName.Length - 1
                    folderKey += folderName(i) & "/"

                Next
                ' folderKey = folderKey & "/"    'end the folder name with "/"
                Dim request As PutObjectRequest = New PutObjectRequest()
                request.BucketName = bucketName
                request.StorageClass = S3StorageClass.Standard
                request.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None
                ' request.CannedACL = S3CannedACL.BucketOwnerFullControl
                request.Key = folderKey
                request.ContentBody = String.Empty
                s3Client.PutObject(request)
            End If
        Catch ex As Exception
            returnval = ex.Message
        End Try
    Catch ex As AmazonS3Exception
        returnval = ex.Message
    End Try
    Return returnval
    End Function
    Public Function AddFileToFolder(FileName As String, bucketName As String, folderName As String) As String
    Dim returnval As String = ""
    Try
        Try
            If Not AmazonS3Util.DoesS3BucketExist(s3Client, bucketName) Then
                Dim fname() As String = folderName.Split("/")
                CreateFolder(bucketName, fname)
            Else
                Dim path As String = FileName
                Dim file As FileInfo = New FileInfo(path)

                Dim key As String = String.Format("{0}/{1}", folderName, file.Name)
                Dim por As PutObjectRequest = New PutObjectRequest()
                por.BucketName = bucketName
                por.StorageClass = S3StorageClass.Standard
                por.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None
                por.CannedACL = S3CannedACL.PublicRead
                por.Key = key
                por.InputStream = file.OpenRead()
                s3Client.PutObject(por)
            End If
        Catch ex As Exception
            returnval = ex.Message
        End Try
    Catch ex As AmazonS3Exception
        returnval = ex.Message
    End Try
    Return returnval
End Function

Public Function ListingFiles(bucketName As String, Optional foldername As String = "/") As ObservableCollection(Of String)
    Dim obsv As New ObservableCollection(Of String)
    Dim delimiter As String = "/"
    If Not foldername.EndsWith(delimiter) Then
        foldername = String.Format("{0}{1}", foldername, delimiter)
    End If
    Try
        Try
            Dim request As New ListObjectsRequest() With {.BucketName = bucketName}
            Do
                Dim response As ListObjectsResponse = s3Client.ListObjects(request)
                For i As Integer = 1 To response.S3Objects.Count - 1
                    Dim entry As S3Object = response.S3Objects(i)
                    If Not foldername = "/" Then
                        If entry.Key.ToString.StartsWith(foldername) Then
                            Dim replacementstring As String = Replace(entry.Key, foldername, "")
                            If Not replacementstring = "" Then
                                obsv.Add(replacementstring)
                            End If
                        End If
                    Else
                        obsv.Add(Replace(entry.Key, foldername, ""))
                    End If
                Next
                If (response.IsTruncated) Then
                    request.Marker = response.NextMarker
                Else
                    request = Nothing
                End If
            Loop Until IsNothing(request)
        Catch ex As AmazonS3Exception

        End Try
    Catch ex As Exception

    End Try
    Return obsv
End Function
Public Function DeleteFileFromFolder(bucketName As String, foldername As String, keyname As String) As String
    Dim returnval As String = ""
    Try
        Try
            If Not AmazonS3Util.DoesS3BucketExist(s3Client, bucketName) Then
                returnval = "Bucket does not exist"
            Else
                foldername = foldername.Replace("\", "/")
                Dim dor As DeleteObjectRequest = New DeleteObjectRequest() With {.BucketName = bucketName, .Key = String.Format("{0}/{1}", foldername, keyname)}
                s3Client.DeleteObject(dor)
            End If
        Catch ex As AmazonS3Exception
            returnval = ex.Message
        End Try
    Catch ex As Exception
        returnval = ex.Message
    End Try
    Return returnval
End Function

Public Function DeleteFolder(bucketName As String, foldername As String) As Boolean
        Try
            If Not foldername.EndsWith("/") Then
                foldername = foldername & "/"
            End If
            Dim dor As DeleteObjectRequest = New DeleteObjectRequest() With {.BucketName = bucketName, .Key = foldername}
            s3Client.DeleteObject(dor)
            Return True
        Catch ex As AmazonS3Exception
            Return False
        End Try

End Function
Public Function GetFileFromFolder(bucketName As String, folderName As String, FileName As String, target As String) As String
    Dim returnval As String = ""
    Try
        Try
            If Not AmazonS3Util.DoesS3BucketExist(s3Client, bucketName) Then
                returnval = "Bucket does not exist"
            Else
                Dim request As ListObjectsRequest = New ListObjectsRequest() With {.BucketName = bucketName}
                Do
                    Dim response As ListObjectsResponse = s3Client.ListObjects(request)
                    For i As Integer = 1 To response.S3Objects.Count - 1
                        Dim entry As S3Object = response.S3Objects(i)
                        If Replace(entry.Key, folderName & "/", "") = FileName Then
                            Dim objRequest As GetObjectRequest = New GetObjectRequest() With {.BucketName = bucketName, .Key = entry.Key}
                            Dim objResponse As GetObjectResponse = s3Client.GetObject(objRequest)
                            objResponse.WriteResponseStreamToFile(target & FileName)
                            Exit Do
                        End If
                    Next
                    If (response.IsTruncated) Then
                        request.Marker = response.NextMarker
                    Else
                        request = Nothing
                    End If
                Loop Until IsNothing(request)
            End If
        Catch ex As AmazonS3Exception
            returnval = ex.Message
        End Try
    Catch ex As Exception
        returnval = ex.Message
    End Try
    Return returnval
End Function

Public Function OpenFile(bucketName As String, folderName As String, FileName As String) As String
    Dim returnval As String = DownloadFile(bucketName, folderName, FileName)
    If returnval = "" Then
        Dim target As String = Path.GetTempPath()
        System.Diagnostics.Process.Start(target & FileName)
    End If
    Return returnval
End Function
Public Function CopyingObject(bucketName As String, folderFile As String, destinationfolder As String) As String
    Dim returnval As String = ""
    Try
        Using s3Client
            Dim request As CopyObjectRequest = New CopyObjectRequest
            request.SourceBucket = bucketName
            request.SourceKey = folderFile.Replace("\", "/")
            request.DestinationBucket = bucketName
            request.DestinationKey = destinationfolder.Replace("\", "/")

            Dim response As CopyObjectResponse = s3Client.CopyObject(request)
        End Using
    Catch s3Exception As AmazonS3Exception
        returnval = s3Exception.Message
    End Try
    Return returnval

End Function
Public Function DownloadFile(bucketName As String, folderName As String, FileName As String) As String
    Dim target As String = Path.GetTempPath()
    Dim returnval As String = ""
    folderName = folderName.Replace("\", "/")
    Try
        Try
            If Not AmazonS3Util.DoesS3BucketExist(s3Client, bucketName) Then
                returnval = "Bucket does not exist"
            Else
                Dim request As ListObjectsRequest = New ListObjectsRequest() With {.BucketName = bucketName}
                Do
                    Dim response As ListObjectsResponse = s3Client.ListObjects(request)
                    For i As Integer = 1 To response.S3Objects.Count - 1
                        Dim entry As S3Object = response.S3Objects(i)
                        If Replace(entry.Key, folderName & "/", "") = FileName Then
                            Dim objRequest As GetObjectRequest = New GetObjectRequest() With {.BucketName = bucketName, .Key = entry.Key}
                            Dim objResponse As GetObjectResponse = s3Client.GetObject(objRequest)
                            objResponse.WriteResponseStreamToFile(target & FileName)
                            Exit Do
                        End If
                    Next
                    If (response.IsTruncated) Then
                        request.Marker = response.NextMarker
                    Else
                        request = Nothing
                    End If
                Loop Until IsNothing(request)
            End If
        Catch ex As AmazonS3Exception
            returnval = ex.Message
        End Try
    Catch ex As Exception
        returnval = ex.Message
    End Try
    Return returnval
End Function

End Class
Run Code Online (Sandbox Code Playgroud)

这个类的用法:

创建桶

 Dim aws As New AWS_S3

  aws.CreateABucket(<bucketname>) 
Run Code Online (Sandbox Code Playgroud)

创建文件夹 Dim fld(1) 作为字符串

                fld(0) = <foldername>
                fld(1) = <subfoldername>
                'List each sub folder as an element in array

                Dim rtrn As String = aws.CreateFolder(<bucketname>, fld)
Run Code Online (Sandbox Code Playgroud)

将文件添加到文件夹

                Dim fld(1) As String                 
                fld(0) = <foldername>
                fld(1) = <subfoldername>
                'List each sub folder as an element in array
                Dim rtrn As String = aws.AddFileToFolder(<local file name>, 
<bucketname>, fld)
Run Code Online (Sandbox Code Playgroud)

列出文件夹中的文件

                 Dim filecol As ObservableCollection(Of String) = aws.ListingFiles(<bucketname>, <foldername>) ...(foldername should end with a "/")
Run Code Online (Sandbox Code Playgroud)