0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AWS】VB.NETで作成したWindowsアプリケーションからAWSを使ってみよう

Last updated at Posted at 2025-01-21

とある歴史のあるシステムにて、拠点間でのデータ交換手法に電子メールを使っている箇所があります。その当時は、メールが身近で使いやすい技術だったのですが、

大量データ送信には向かない
メール喪失時の再送が面倒

などなど、ちょっと使いにくい面もありました。

「動いているソースには安易に手を入れるな」と言われて育ってきた世代ですが、(ソースにおまじないとかコメントあったな...:thinking:)お客さんと作り手双方にメリットがある新しい技術は、積極的に取り入れていきたいものです。

そこで今回は、メールでのデータ交換部分の処理を AWS(S3) に代替すべく手順を調べました。※ソースコードは、Copilot が生成したソースをベースにしています。

メールでのデータ交換をAWS S3に移行する手順

開発環境は、VB.NET(VisualStudio2019) を使っています。
以下のステップとなりました。

1. AWSが使えるようにアカウント作成し、S3上にバケットを作成

※この手順は.NET特有のものではないため、詳細は割愛します。

2. NETからAWS S3が使えるように、NuGetパッケージマネージャーから「AWS SDK for .NET」をインストール

3. 拠点A(データ送信側)S3へのファイルアップロード

次に、データ送信側である拠点Aから、ファイルをS3にアップロードします。以下のVB.NETコードで実現できます。

vb
    Imports Amazon.S3
    Imports Amazon.S3.Transfer

    Module Module1
        Sub Main()
            Dim bucketName As String = "your-bucket-name"
            Dim accessKey As String = "your-access-key"
            Dim secretKey As String = "your-secret-key"
            Dim filePath As String = "C:\path\to\your\datafile.txt"
  
            Dim s3Client As IAmazonS3 = New AmazonS3Client(accessKey, secretKey, RegionEndpoint.YourRegion)
            Dim fileTransferUtility As New TransferUtility(s3Client)

            fileTransferUtility.Upload(filePath, bucketName, "your-file-key")
            Console.WriteLine("File uploaded successfully.")
        End Sub
    End Module

現状のシステムでは、メールで送るファイルをローカルに保存しているため、そのファイルをS3に転送するのは結構簡単に出来そうです。(エラー処理はとりあえず置いておきます。)

4. 拠点B(データ受信側)でS3からファイルをダウンロード

データ受信側である拠点Bでは、S3からファイルをダウンロードする処理を実装します。以下のコードでダウンロードを実現できます。

vb
Imports Amazon
Imports Amazon.S3
Imports Amazon.S3.Model
Imports System.IO

Module Module1
    Sub Main()
        Dim bucketName As String = "your-bucket-name"
        Dim keyName As String = "your-file-key"
        Dim filePath As String = "C:\path\to\save\file.txt"
        Dim accessKey As String = "your-access-key"
        Dim secretKey As String = "your-secret-key"

        Dim s3Client As IAmazonS3 = New AmazonS3Client(accessKey, secretKey, RegionEndpoint.YourRegion)

        Try
            Dim request As New GetObjectRequest() With {
                .BucketName = bucketName,
                .Key = keyName
            }

            Using response As GetObjectResponse = s3Client.GetObject(request)
                Using responseStream As Stream = response.ResponseStream
                    Using fileStream As FileStream = File.Create(filePath)
                        responseStream.CopyTo(fileStream)
                    End Using
                End Using
            End Using

            Console.WriteLine("File downloaded successfully to " & filePath)
        Catch ex As AmazonS3Exception
            Console.WriteLine("Error encountered ***. Message: '{0}' when writing an object", ex.Message)
        End Try
    End Sub
End Module

このコードでは、S3から指定したファイルをダウンロードし、ローカルに保存します。特に難しいことなく実装できると思います。

Smart Design 高橋

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?