Sample vb.net code to upload file to Amazon S3 sto

2019-08-15 04:04发布

问题:

I am looking for a sample vb.net project that will upload a file to Amazon S3 storage.

I am happy to use any official Amazon API, but do not want to use a 3rd party product.

Regards, Leigh

回答1:

A bit late to the party, but Here is a class I use to deal with Amazon S3 storage. It will handle anything your are trying to accomplish in regards to files on 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

Usage for this class:

CREATE BUCKET

 Dim aws As New AWS_S3

  aws.CreateABucket(<bucketname>) 

CREATE FOLDER 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.CreateFolder(<bucketname>, fld)

ADD FILE TO FOLDER

                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)

LIST FILES IN FOLDER

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


回答2:

Last time I checked, I found a lot of useful information over at CodeProject: Beginning with Amazon S3 by StormSpirit Team.

It's in C#, but you can easily convert it to VB.NET with online-converter, i.e. Telerik's Code Converter