如何使用SSIS将文件上传到S3?

时间:2019-05-10 18:36:26

标签: c# .net amazon-s3 ssis

这是几个小时前的问题,我只是想节省某人的所有精力,以解决这个问题。

有关如何使用SSIS中的脚本任务将文件上传到S3的基本代码。确保将Amazon.S3.dllAmazon.Core.dll添加到引用中。

using Amazon.S3;
using Amazon.S3.Transfer;
using Amazon.S3.Model;
using System.IO;

namespace tests
{
    class Program
    {
        static ScriptMain()
        {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        }

        static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string path = @"Path to DLLs";
            if (args.Name.Contains("AWSSDK.Core"))
            {
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "AWSSDK.Core.dll"));
            }
            if (args.Name.Contains("AWSSDK.S3"))
            {
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "AWSSDK.S3.dll"));
            }
            return null;
        }

        public void Main()
        {
            string filePath = @"Path to file to be uploaded";
            string bucket = "bllcrdtanalytics/Optimization/test"; //bucket name example
            IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2);

            PutObjectRequest putRequest = new PutObjectRequest();
            putRequest.FilePath = filePath;
            putRequest.Key = Path.GetFileName(filePath);
            putRequest.BucketName = bucket;
            PutObjectResponse response = s3Client.PutObject(putRequest);

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

0 个答案:

没有答案