Implementation for accessing S3
Inheritance: AmazonServiceClient, IAmazonS3
示例#1
10
        public static Boolean SendImageToS3(String key, Stream imageStream)
        {
            var success = false;

            using (var client = new AmazonS3Client(RegionEndpoint.USWest2))
            {
                try
                {
                    PutObjectRequest request = new PutObjectRequest()
                    {
                        InputStream = imageStream,
                        BucketName = BucketName,
                        Key = key
                    };

                    client.PutObject(request);
                    success = true;
                }

                catch (Exception ex)
                {
                    // swallow everything for now.
                }
            }

            return success;
        }
        public async Task UploadDataFileAsync(string data, string storageKeyId, string storageSecret, string region)
        {
            var regionIdentifier = RegionEndpoint.GetBySystemName(region);
            using (var client = new AmazonS3Client(storageKeyId, storageSecret, regionIdentifier))
            {
                try
                {
                    var putRequest1 = new PutObjectRequest
                    {
                        BucketName = AwsBucketName,
                        Key = AwsBucketFileName,
                        ContentBody = data,
                        ContentType = "application/json"
                    };

                    await client.PutObjectAsync(putRequest1);
                }
                catch (AmazonS3Exception amazonS3Exception)
                {
                    if (amazonS3Exception.ErrorCode != null &&
                        (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                    {
                        throw new SecurityException("Invalid Amazon S3 credentials - data was not uploaded.", amazonS3Exception);
                    }

                    throw new HttpRequestException("Unspecified error attempting to upload data: " + amazonS3Exception.Message, amazonS3Exception);
                }

                var response = await client.GetObjectAsync(AwsBucketName, AwsBucketFileName);
                using (var reader = new StreamReader(response.ResponseStream))
                {
                    Debug.WriteLine(await reader.ReadToEndAsync());
                }
            }
        }
 public async Task UploadFile(string name,IStorageFile storageFile)
 {            
     var s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
     var transferUtilityConfig = new TransferUtilityConfig
     {                
         ConcurrentServiceRequests = 5,                
         MinSizeBeforePartUpload = 20 * MB_SIZE,
     };
     try
     {
         using (var transferUtility = new TransferUtility(s3Client, transferUtilityConfig))
         {
             var uploadRequest = new TransferUtilityUploadRequest
             {
                 BucketName = ExistingBucketName,
                 Key = name,
                 StorageFile = storageFile,
                 // Set size of each part for multipart upload to 10 MB
                 PartSize = 10 * MB_SIZE
             };
             uploadRequest.UploadProgressEvent += OnUploadProgressEvent;
             await transferUtility.UploadAsync(uploadRequest);
         }
     }
     catch (AmazonServiceException ex)
     {
       //  oResponse.OK = false;
      //   oResponse.Message = "Network Error when connecting to AWS: " + ex.Message;
     }
 }
        public ImportIntoS3Tasks()
        {
            appHost = new BasicAppHost().Init();

            var s3Client = new AmazonS3Client(AwsConfig.AwsAccessKey, AwsConfig.AwsSecretKey, RegionEndpoint.USEast1);
            s3 = new S3VirtualPathProvider(s3Client, AwsConfig.S3BucketName, appHost);
        }
示例#5
1
 public void DeleteFile(String filename)
 {
     String key = filename;
     var amazonClient = new AmazonS3Client(_keyPublic, _keySecret);
     var deleteObjectRequest = new DeleteObjectRequest { BucketName = _bucket, Key = key };
     var response = amazonClient.DeleteObject(deleteObjectRequest);
 }
 public Guid? GetTargetAppVersion(Guid targetKey, Guid appKey)
 {
     try
     {
         using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
         {
             using (var res = client.GetObject(new GetObjectRequest()
             {
                 BucketName = Context.BucketName,
                 Key = GetTargetAppVersionInfoPath(targetKey, appKey),
             }))
             {
                 using (var stream = res.ResponseStream)
                 {
                     return Utils.Serialisation.ParseKey(stream);
                 }
             }
         }
     }
     catch (AmazonS3Exception awsEx)
     {
         if (awsEx.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             return null;
         }
         else
         {
             throw new DeploymentException(string.Format("Failed getting version for app with key \"{0}\" and target with the key \"{1}\".", appKey, targetKey), awsEx);
         }
     }
 }
示例#7
0
        public void TestSerializingExceptions()
        {
            using(var client = new Amazon.S3.AmazonS3Client())
            {
                try
                {
                    var fakeBucketName = "super.duper.fake.bucket.name.123." + Guid.NewGuid().ToString();
                    client.ListObjects(fakeBucketName);
                }
                catch(AmazonS3Exception e)
                {
                    TestException(e);
                }

                var s3pue = CreateS3PostUploadException();
                TestException(s3pue);

                var doe = CreateDeleteObjectsException();
                TestException(doe);

                var aace = new AdfsAuthenticationControllerException("Message");
                TestException(aace);

#pragma warning disable 618

                var ccre = new CredentialCallbackRequiredException("Message");
                TestException(ccre);

                var afe = new AuthenticationFailedException("Message");
                TestException(afe);

#pragma warning restore 618

            }
        }
示例#8
0
        public static async System.Threading.Tasks.Task <bool> Delete(this string fileNameGuid, string _awsAccessKey, string _awsSecretKey, string _bucketName)
        {
            IAmazonS3 client;
            var       s3Client = RegionEndpoint.USEast1;

            try
            {
                using (client = new Amazon.S3.AmazonS3Client(_awsAccessKey, _awsSecretKey, s3Client))
                {
                    DeleteObjectRequest request = new DeleteObjectRequest()
                    {
                        BucketName = _bucketName,
                        Key        = fileNameGuid,
                    };

                    await client.DeleteObjectAsync(request);

                    client.Dispose();
                }
            }
            catch (Exception exception)
            {
                Logging.Log("Upload Documents failure", "S3 File upload Extension Method", exception);
                return(false);
            }

            return(true);
        }
示例#9
0
文件: AwsCmm.cs 项目: chae87/First
        /// <summary>AWS S3 여러 객체 삭제</summary>
        public DeleteObjectsResponse DeleteObjectList(List<string> pKeyList)
        {
            try
            {
                using (AmazonS3Client client = new AmazonS3Client())
                {
                    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest();
                    multiObjectDeleteRequest.BucketName = strAwsBucketName;

                    foreach (string key in pKeyList)
                    {
                        multiObjectDeleteRequest.AddKey(key);
                    }

                    DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest);

                    //response.DeleteErrors.Count = 실패한 삭제 객체
                    //response.DeletedObjects.Count = 성공한 삭제 객체
                    //.Key, .Code, .Message로 정보 확인 가능.
                    return response;
                }
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                throw amazonS3Exception;
            }
        }
示例#10
0
 public EnvioS3(StringBuilder log, string arquivo, string accessKey, string secretKey, string bucketName)
 {
     _log = log;
     _arquivo = arquivo;
     _bucketName = bucketName;
     _client = new AmazonS3Client(accessKey, secretKey);
 }
示例#11
0
        public S3Reader2(NameValueCollection args )
        {
            s3config = new AmazonS3Config();

            buckets = args["buckets"];
            vpath = args["prefix"];

            asVpp = NameValueCollectionExtensions.Get(args, "vpp", true);

            Region = args["region"] ?? "us-east-1";

            s3config.UseHttp = !NameValueCollectionExtensions.Get(args, "useSsl", false);

            if (!string.IsNullOrEmpty(args["accessKeyId"]) && !string.IsNullOrEmpty(args["secretAccessKey"])) {
                S3Client = new AmazonS3Client(args["accessKeyId"], args["secretAccessKey"], s3config);
            } else {

                S3Client = new AmazonS3Client(null, s3config);
            }

            includeModifiedDate = NameValueCollectionExtensions.Get(args, "includeModifiedDate", includeModifiedDate);

            includeModifiedDate = NameValueCollectionExtensions.Get(args, "checkForModifiedFiles", includeModifiedDate);

            RequireImageExtension = NameValueCollectionExtensions.Get(args, "requireImageExtension", RequireImageExtension);
            UntrustedData = NameValueCollectionExtensions.Get(args, "untrustedData", UntrustedData);
            CacheUnmodifiedFiles = NameValueCollectionExtensions.Get(args, "cacheUnmodifiedFiles", CacheUnmodifiedFiles);
        }
示例#12
0
        public void TestSerializingExceptions()
        {
            using (var client = new Amazon.S3.AmazonS3Client())
            {
                try
                {
                    var fakeBucketName = "super.duper.fake.bucket.name.123." + Guid.NewGuid().ToString();
                    client.ListObjects(fakeBucketName);
                }
                catch (AmazonS3Exception e)
                {
                    TestException(e);
                }

                var s3pue = CreateS3PostUploadException();
                TestException(s3pue);

                var doe = CreateDeleteObjectsException();
                TestException(doe);

                var aace = new AdfsAuthenticationControllerException("Message");
                TestException(aace);

#pragma warning disable 618

                var ccre = new CredentialCallbackRequiredException("Message");
                TestException(ccre);

                var afe = new AuthenticationFailedException("Message");
                TestException(afe);

#pragma warning restore 618
            }
        }
示例#13
0
        public void Store(string key, byte[] data)
        {
            Amazon.S3.AmazonS3Client client = getS3Client();
            var resTask = client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest()
            {
                BucketName  = bucketName,
                Key         = key,
                InputStream = new MemoryStream(data),
            });
            bool      error          = false;
            Exception innerException = null;

            try
            {
                resTask.Wait();
            }
            catch (Exception ex)
            {
                innerException = ex;
            }
            if (error || resTask.Result.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                if (innerException == null)
                {
                    innerException = resTask.Exception;
                }
                var newEx = new Exception("Store failed, error code: " + resTask.Result.HttpStatusCode, innerException);

                Logger.Error(newEx, "Error storing in S3 bucket, bucket name=" + bucketName + ", key=" + key);
                throw newEx;
            }
        }
示例#14
0
        public void TestLocation()
        {
            foreach (var location in new S3Region[] { S3Region.USW1, S3Region.EUC1 })
            {
                string bucketName = null;
                var region = RegionEndpoint.GetBySystemName(location.Value);

                using (var client = new AmazonS3Client(region))
                {
                    try
                    {
                        bucketName = UtilityMethods.SDK_TEST_PREFIX + DateTime.Now.Ticks;

                        client.PutBucket(new PutBucketRequest
                        {
                            BucketName = bucketName,
                            BucketRegion = location
                        });

                        var returnedLocation = client.GetBucketLocation(new GetBucketLocationRequest
                        {
                            BucketName = bucketName
                        }).Location;

                        Assert.AreEqual(location, returnedLocation);
                    }
                    finally
                    {
                        if (bucketName != null)
                            AmazonS3Util.DeleteS3BucketWithObjects(client, bucketName);
                    }
                }
            }
        }
示例#15
0
        static void Main(string[] args)
        {
            //https://github.com/aws/aws-sdk-net/issues/499
            Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", AWS_Consts.ACCESS_KEY);
            Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", AWS_Consts.SECRET_ACCESS_KEY);
            Environment.SetEnvironmentVariable("AWS_REGION", "us-east-1");
            // https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpNET.html
            using (var s3 = new Amazon.S3.AmazonS3Client(Amazon.RegionEndpoint.USEast1)){
                var request = new PutObjectRequest {
                    BucketName = "aws-lightsail",
                    Key        = "keyName",
                    FilePath   = "/SwiftLanguage.pdf"
                };

                request.Metadata.Add("x-amz-meta-title", "swift");
                var response2 = s3.PutObjectAsync(request);
                Console.WriteLine(response2.Result);
            }
            //     var region = "us-east-1";
            //     var request = (HttpWebRequest)WebRequest.Create(string.Format("https://aws-lightsail.s3.{0}.amazonaws.com",region));
            //     request.Method = "POST";
            //     request.Headers.Add("AWSAccessKeyId",AWS_Consts.ACCESS_KEY);

            //     using(var response = (HttpWebResponse)request.GetResponse()){
            //         if(response.StatusCode==HttpStatusCode.OK){
            //             using(var stream = response.GetResponseStream()){
            //                 Console.WriteLine(new StreamReader(stream).ReadToEnd());
            //             }
            //         }
            //         else throw new WebException("Error: "+response.StatusCode);
            //     }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AmazonS3VirtualDirectory"/> class.
 /// </summary>
 /// <param name="provider">The virtual path provider.</param>
 /// <param name="virtualPath">The virtual path to the resource represented by this instance.</param>
 public AmazonS3VirtualDirectory(AmazonS3VirtualPathProvider provider, string virtualPath)
     : base(virtualPath)
 {
     _client = new AmazonS3Client(new AmazonS3Config {ServiceURL = "s3.amazonaws.com", CommunicationProtocol = Protocol.HTTP});
     _provider = provider;
     _virtualPath = virtualPath;
 }
示例#17
0
 public virtual void Init()
 {
     _bucket = "kalixtest";
     _client = AmazonTestsHelper.SetupBlob(_bucket, "kalix-leo-tests\\AmazonStoreTests.testdata");
     _location = new StoreLocation("kalix-leo-tests", "AmazonStoreTests.testdata");
     _store = new AmazonStore(_client, _bucket);
 }
示例#18
0
        private void CreateAndCheckTestBucket()
        {
            TestBucketIsReady = false;
            USEast1Client = new AmazonS3Client(RegionEndpoint.USEast1);
            USWest1Client = new AmazonS3Client(RegionEndpoint.USWest1);
            var sessionCredentials = new AmazonSecurityTokenServiceClient().GetSessionToken().Credentials;
            USEast1ClientWithSessionCredentials = new AmazonS3Client(sessionCredentials, RegionEndpoint.USEast1);

            TestBucket = USWest1Client.ListBuckets().Buckets.Find(bucket => bucket.BucketName.StartsWith(BucketPrefix));
            if (TestBucket == null)
            {
                // add ticks to bucket name because the bucket namespace is shared globally
                var bucketName = BucketPrefix + DateTime.Now.Ticks;
                // Create the bucket but don't run the test.
                // If the bucket is ready the next time this test runs we'll test then.
                USWest1Client.PutBucket(new PutBucketRequest()
                {
                    BucketRegion = S3Region.USW1,
                    BucketName = bucketName,
                });
            }
            else if (TestBucket.CreationDate.AddHours(TemporaryRedirectMaxExpirationHours) < DateTime.Now)
            {
                BucketRegionDetector.BucketRegionCache.Clear();
                TestBucketIsReady = true;
            }
        }
示例#19
0
        public static GetObjectResponse GetFile(AwsCommonParams commonParams,
            string bucketName, string filePath)
        {
            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            // This also implements behavior consistent with the
            // edit counterpart routine for verification purposes
            if (filePath.StartsWith("/"))
                filePath = filePath.Substring(1);

            using (var s3 = new Amazon.S3.AmazonS3Client(
                commonParams.ResolveCredentials(),
                commonParams.RegionEndpoint))
            {
                var s3Requ = new Amazon.S3.Model.GetObjectRequest
                {
                    BucketName = bucketName,
                    //Prefix = filePath,
                    Key = filePath,
                };

                //var s3Resp = s3.ListObjects(s3Requ);
                try
                {
                    var s3Resp = s3.GetObject(s3Requ);

                    return s3Resp;
                }
                catch (AmazonS3Exception ex)
                    when (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return null;
                }
            }
        }
示例#20
0
    // Use this for initialization
    void Start () {
        // ResultText is a label used for displaying status information
        Debug.Log("hallo1");

        


    Debug.Log("hallo2");
        S3Client = new AmazonS3Client(Credentials);
        Debug.Log("hallo3");
        ResultText.text = "Fetching all the Buckets";
        S3Client.ListBucketsAsync(new ListBucketsRequest(), (responseObject) =>
        {
            ResultText.text += "\n";
            if (responseObject.Exception == null)
            {
                ResultText.text += "Got Response \nPrinting now \n";
                responseObject.Response.Buckets.ForEach((s3b) =>
                {
                    ResultText.text += string.Format("bucket = {0}, created date = {1} \n",
                    s3b.BucketName, s3b.CreationDate);
                });
            }
            else
            {
                ResultText.text += "Got Exception \n";
            }
        });
    }
        /// <summary>
        /// Called when this Adapter is Started.
        /// Used to Setup the Connection to AWS S3
        /// </summary>
        public override void OnInit()
        {
            Amazon.RegionEndpoint region = null;
            if (awsAccessKeyId == null)
            {
                LOGERROR("Missing Parameter awsAccessLeyId is required");
            }
            if (awsSecretAccessKey == null)
            {
                LOGERROR("Missing Parameter awsSecretAccessKey is required");
            }
            if (awsBucket == null)
            {
                LOGERROR("Missing Parameter awsBucket is required");
            }

            if (awsRegion == null)
            {
                LOGERROR("Missing Parameter awsRegion is required");
            }
            else
            {
                region = Amazon.RegionEndpoint.GetBySystemName(awsRegion);
                if (region == null)
                {
                    LOGERROR("awsRegion '" + awsRegion + "' not recognized");
                }
            }
            LOGINFO("Creating AWS S3 Client with given credentials");
            s3Client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, region);
        }
示例#22
0
 /// <summary>
 /// Default settings: CommunicationProtocol=HTTP, VirtualFileSystemPrefix = "~/s3", SlidingExpiration = 1h, AbsoluteExpiration = maxvalue
 /// No bucket filtering is performed, so any amazon-hosted bucket can be accessed through this provider unless you add a bucket filter.
 /// </summary>
 /// <param name="bucketFilterCallback">You should validate that the requested bucket is your own. If you only want one bucket, just prefix your bucket to the path.</param>
 /// <param name="fastMode">If true, existence of bucket and key is assumed as long as prefix is present. Also, no modified date information is provided to the image resizer, so the cache never gets updated. Requires 1 request instead of 2 to download the image.</param>
 public S3VirtualPathProvider(RewriteBucketAndKeyPath bucketFilterCallback, Boolean fastMode)
     : base()
 {
     this.s3Client = new AmazonS3Client(null,new AmazonS3Config() { UseSecureStringForAwsSecretKey = false, CommunicationProtocol = Amazon.S3.Model.Protocol.HTTP });
     this.PreS3RequestFilter += bucketFilterCallback;
     this.FastMode = fastMode;
 }
示例#23
0
        public S3Reader(NameValueCollection args )
        {
            var S3Config = new AmazonS3Config();

            buckets = args["buckets"];
            vpath = args["prefix"];

            asVpp = NameValueCollectionExtensions.Get(args, "vpp", true);

            S3Config.CommunicationProtocol = NameValueCollectionExtensions.Get(args, "useSsl", false) ? Amazon.S3.Model.Protocol.HTTPS : Amazon.S3.Model.Protocol.HTTP;
            S3Config.UseSecureStringForAwsSecretKey = false;

            if (!string.IsNullOrEmpty(args["accessKeyId"]) && !string.IsNullOrEmpty(args["secretAccessKey"])) {
                S3Client = new AmazonS3Client(args["accessKeyId"], args["secretAccessKey"], S3Config);
            } else {

                S3Client = new AmazonS3Client(null,S3Config);
            }

            includeModifiedDate = NameValueCollectionExtensions.Get(args, "includeModifiedDate", includeModifiedDate);

            includeModifiedDate = NameValueCollectionExtensions.Get(args, "checkForModifiedFiles", includeModifiedDate);

            RequireImageExtension = NameValueCollectionExtensions.Get(args, "requireImageExtension", RequireImageExtension);
            UntrustedData = NameValueCollectionExtensions.Get(args, "untrustedData", UntrustedData);
            CacheUnmodifiedFiles = NameValueCollectionExtensions.Get(args, "cacheUnmodifiedFiles", CacheUnmodifiedFiles);
        }
示例#24
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (upload.PostedFile != null && upload.PostedFile.ContentLength > 0 && !string.IsNullOrEmpty(Request["awsid"]) && !string.IsNullOrEmpty(Request["awssecret"]) && !string.IsNullOrEmpty(this.bucket.Text))
            {
                var name = "s3readersample/" + ImageUploadHelper.Current.GenerateSafeImageName(upload.PostedFile.InputStream, upload.PostedFile.FileName);

                var client = new Amazon.S3.AmazonS3Client(Request["awsid"], Request["awssecret"], Amazon.RegionEndpoint.EUWest1);

                //For some reason we have to buffer the file in memory to prevent issues... Need to research further
                var ms = new MemoryStream();
                upload.PostedFile.InputStream.CopyTo(ms);
                ms.Seek(0, SeekOrigin.Begin);

                var request = new Amazon.S3.Model.PutObjectRequest()
                {
                    BucketName = this.bucket.Text, Key = name, InputStream = ms, CannedACL = Amazon.S3.S3CannedACL.PublicRead
                };


                var response = client.PutObject(request);
                if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    result.Text = "Successfully uploaded " + name + "to bucket " + this.bucket.Text;
                }
                else
                {
                    result.Text = response.HttpStatusCode.ToString();
                }
            }
        }
示例#25
0
        public byte[] Get(string key)
        {
            Amazon.S3.AmazonS3Client client = getS3Client();
            var resTask = client.GetObjectAsync(bucketName, key);

            try
            {
                resTask.Wait();
            }
            catch (AggregateException ex)
            {
                BatchProcessor.Logging.Logger.Error(ex, "Error retreiving an item from S3 bucket, bucket name=" + bucketName + ", item key=" + key);
                //TODO: Check the first exception, and make sure that it is 404 not found.
                return(null);
            }
            byte[] res;
            using (resTask.Result.ResponseStream)
            {
                using (MemoryStream mem = new MemoryStream())
                {
                    resTask.Result.ResponseStream.CopyTo(mem);
                    res = mem.ToArray();
                }
            }
            return(res);
        }
示例#26
0
        static void Main(string[] args)
        {
            try
            {
                var client = new AmazonS3Client();

                PutObjectResponse putResponse = client.PutObject(new PutObjectRequest
                {
                    BucketName = BUCKET_NAME,
                    FilePath = TEST_FILE
                });

                GetObjectResponse getResponse = client.GetObject(new GetObjectRequest
                {
                    BucketName = BUCKET_NAME,
                    Key = TEST_FILE
                });

                getResponse.WriteResponseStreamToFile(@"c:\talk\" + TEST_FILE);


                var url = client.GetPreSignedURL(new GetPreSignedUrlRequest
                {
                    BucketName = BUCKET_NAME,
                    Key = TEST_FILE,
                    Expires = DateTime.Now.AddHours(1)
                });

                OpenURL(url);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#27
0
        public void ConfigureDevelopmentServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // Add S3 to the ASP.NET Core dependency injection framework.
            var settings = new LocalWindowsEnvironmentSettings(Configuration);

            services.AddSingleton <IEnvironmentSettings>(settings);

            var s3 = new Amazon.S3.AmazonS3Client(settings.AWSAccessKey, settings.AWSSecret, Amazon.RegionEndpoint.USWest2);

            services.AddSingleton <IAmazonS3>(s3);

            var emailService = new AmazonSimpleEmailServiceClient(settings.AWSAccessKey, settings.AWSSecret, Amazon.RegionEndpoint.USWest2);

            //var emailClient = new Mock<IAmazonSimpleEmailService>();
            //emailClient.Setup(e => e.SendEmailAsync(It.IsAny<SendEmailRequest>(), new System.Threading.CancellationToken()))
            //    .Callback<SendEmailRequest, System.Threading.CancellationToken>((req, token) =>
            //      {
            //          Console.WriteLine("Email Sent:");
            //          Console.WriteLine(req.Message.Body);
            //      })
            //    .Returns(Task.FromResult(new SendEmailResponse()));
            services.AddSingleton <IAmazonSimpleEmailService>(emailService);
        }
示例#28
0
        public S3TraceListener(string initializeData)
        {
            string[] commands = initializeData.Split(',');
            foreach (var command in commands)
            {
                string[] subcommand = command.Split('=');
                switch (subcommand[0])
                {
                    case "logfile":
                        LogFileName = string.Format("{0}.{1}.log", GetUnixTime(), subcommand[1]);
                        break;
                    case "bucket":
                        _bucketName = subcommand[1];
                        break;
                }
            }

            if (LogFileName == null || _bucketName == null)
            {
                throw new Exception("Not valid parameters. Pass logfile=,bucketname=.");
            }

            if (_amazonConfig == null)
            {
                _amazonConfig = new EnvironmentAWSCredentials();
                _s3Client = new AmazonS3Client(_amazonConfig);
                _stringFile = new List<string>();

            }
        }
        public IComputeNode GetComputeNode()
        {
            IComputeNode compute_node = null;
            try
            {
                //amazon client
                using (var client = new AmazonS3Client())
                {
                    //download request
                    using (var response = client.GetObject(new GetObjectRequest()
                        .WithBucketName(AmazonBucket)
                        .WithKey(BermudaConfig)))
                    {
                        using (StreamReader reader = new StreamReader(response.ResponseStream))
                        {
                            //read the file
                            string data = reader.ReadToEnd();

                            //deserialize
                            compute_node = new ComputeNode().DeserializeComputeNode(data);
                            if(compute_node.Catalogs.Values.Cast<ICatalog>().FirstOrDefault().CatalogMetadata.Tables.FirstOrDefault().Value.DataType == null)
                                compute_node.Catalogs.Values.Cast<ICatalog>().FirstOrDefault().CatalogMetadata.Tables.FirstOrDefault().Value.DataType = typeof(UDPTestDataItems);
                            compute_node.Init(CurrentInstanceIndex, AllNodeEndpoints.Count());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            return compute_node;
        }
示例#30
0
		public static IEnumerable<string> ListFiles(string bucketName, string accessKeyID, string secretAccessKey)
		{
			List<string> fileNames = new List<string>();
			try
			{
				var s3Client = new AmazonS3Client(accessKeyID, secretAccessKey, Amazon.RegionEndpoint.USEast1);
				ListObjectsRequest request = new ListObjectsRequest 
				{ 
					BucketName = bucketName 
				};

				while (request != null)
				{
					ListObjectsResponse response = s3Client.ListObjects(request);

					foreach (S3Object entry in response.S3Objects)
					{
						fileNames.Add(entry.Key);
					}

					if (response.IsTruncated)
					{
						request.Marker = response.NextMarker;
					}
					else
					{
						request = null;
					}
				}
			}
			catch (Exception e)
			{
			}
			return fileNames;
		}
示例#31
0
        public S3Storage()
        {
            const string filename = "keyxml.pk";
            var path = WebServerPathUtils.GetPathTo(Path.Combine("bin", filename));
            var f = new FileInfo(path);

            if (f.Exists)
            {
                using (var file = f.OpenRead())
                {
                    var keyString = new StreamReader(file).ReadToEnd();
                    _algorithm = RSA.Create();
                    _algorithm.FromXmlString(keyString);

                    var encryptionMaterials = new EncryptionMaterials(_algorithm);
                    try
                    {
                        _client = new AmazonS3EncryptionClient(encryptionMaterials);

                        var bucket = new S3DirectoryInfo(_client, PdfDocumentsBucketName);
                        if (!bucket.Exists)
                        {
                            bucket.Create();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Unable to initialize S3 client\n" + ex);
                    }
                }
            }
        }
示例#32
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (upload.PostedFile != null && upload.PostedFile.ContentLength > 0 && !string.IsNullOrEmpty(Request["awsid"]) && !string.IsNullOrEmpty(Request["awssecret"]) && !string.IsNullOrEmpty(this.bucket.Text))
            {
                var name = "s3readersample/" + ImageUploadHelper.Current.GenerateSafeImageName(upload.PostedFile.InputStream, upload.PostedFile.FileName);

                var client = new Amazon.S3.AmazonS3Client(Request["awsid"], Request["awssecret"], Amazon.RegionEndpoint.EUWest1);

                //For some reason we have to buffer the file in memory to prevent issues... Need to research further
                var ms = new MemoryStream();
                upload.PostedFile.InputStream.CopyTo(ms);
                ms.Seek(0, SeekOrigin.Begin);

                var request = new Amazon.S3.Model.PutObjectRequest() {  BucketName = this.bucket.Text, Key = name, InputStream = ms, CannedACL = Amazon.S3.S3CannedACL.PublicRead };

                var response = client.PutObject(request);
                if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    result.Text = "Successfully uploaded " + name + "to bucket " + this.bucket.Text;

                }
                else
                {
                    result.Text = response.HttpStatusCode.ToString();
                }
            }
        }
示例#33
0
            public S3(string accessKeyId, string secretAccessKey, string serviceUrl)
            {
                Amazon.S3.AmazonS3Config s3Config = new Amazon.S3.AmazonS3Config();
                s3Config.ServiceURL = serviceUrl;

                this.S3Client = new Amazon.S3.AmazonS3Client(accessKeyId, secretAccessKey, s3Config);
            }
 public AmazonMultiUploadStream(AmazonS3Client client, string bucket, string key, Metadata metadata)
 {
     _client = client;
     _bucket = bucket;
     _key = key;
     _metadata = metadata;
 }
        public IComputeNode GetComputeNode()
        {
            IComputeNode compute_node = null;
            try
            {
                //amazon client
                using (var client = new AmazonS3Client())
                {
                    //download request
                    using (var response = client.GetObject(new GetObjectRequest()
                        .WithBucketName(AmazonBucket)
                        .WithKey(BermudaConfig)))
                    {
                        using (StreamReader reader = new StreamReader(response.ResponseStream))
                        {
                            //read the file
                            string data = reader.ReadToEnd();

                            //deserialize
                            compute_node = new ComputeNode().DeserializeComputeNode(data);
                            compute_node.Init(CurrentInstanceIndex, AllNodeEndpoints.Count());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            return compute_node;
        }
示例#36
0
        public override void Configure(Container container)
        {
            JsConfig.EmitCamelCaseNames = true;
            Plugins.Add(new RazorFormat());

            //Comment out 2 lines below to change to use local FileSystem instead of S3
            var s3Client = new AmazonS3Client(AwsConfig.AwsAccessKey, AwsConfig.AwsSecretKey, RegionEndpoint.USEast1);
            VirtualFiles = new S3VirtualPathProvider(s3Client, AwsConfig.S3BucketName, this);

            container.Register<IPocoDynamo>(c => new PocoDynamo(AwsConfig.CreateAmazonDynamoDb()));
            var db = container.Resolve<IPocoDynamo>();
            db.RegisterTable<Todos.Todo>();
            db.RegisterTable<EmailContacts.Email>();
            db.RegisterTable<EmailContacts.Contact>();
            db.InitSchema();

            //AWS Auth
            container.Register<ICacheClient>(new DynamoDbCacheClient(db, initSchema:true));
            container.Register<IAuthRepository>(new DynamoDbAuthRepository(db, initSchema:true));
            Plugins.Add(CreateAuthFeature());

            //EmailContacts
            ConfigureSqsMqServer(container);
            ConfigureEmailer(container);
            Plugins.Add(new ValidationFeature());
            container.RegisterValidators(typeof(EmailContacts.CreateContact).Assembly);
        }
示例#37
0
 public static void DeleteBucket(string bucketName)
 {
     using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
     {
         client.DeleteBucket(bucketName);
     }
 }
示例#38
0
        public void TestPostUpload()
        {
            var region = RegionEndpoint.USWest1;

            using (var client = new AmazonS3Client(region))
            {
                var bucketName = S3TestUtils.CreateBucket(client);
                client.PutACL(new PutACLRequest
                {
                    BucketName = bucketName,
                    CannedACL = S3CannedACL.BucketOwnerFullControl
                });

                var credentials = GetCredentials(client);
                try
                {
                    var response = testPost("foo/bar/content.txt", bucketName, testContentStream("Line one\nLine two\nLine three\n"), "", credentials, region);
                    Assert.IsNotNull(response.RequestId);
                    Assert.IsNotNull(response.HostId);
                    Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
                }
                finally
                {
                    AmazonS3Util.DeleteS3BucketWithObjects(client, bucketName);
                }
            }
        }
示例#39
0
        public static async Task <string> UploadImageBlobAmazonS3(string fileName, string blobName, List <KeyValuePair <string, string> > metadataTags)
        {
            string bucketName   = ConfigurationManager.AppSettings["AWSMediaItemsContainerName"];
            string accessKey    = ConfigurationManager.AppSettings["AWSAccessKey"];
            string accessSecret = ConfigurationManager.AppSettings["AWSAccessSecret"];

            using (var client = new Amazon.S3.AmazonS3Client(accessKey, accessSecret, Amazon.RegionEndpoint.APSoutheast2))
            {
                //upload: http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpNET.html
                try
                {
                    PutObjectRequest putRequest1 = new PutObjectRequest
                    {
                        BucketName = bucketName,
                        Key        = "images/" + blobName,
                        FilePath   = fileName,
                        CannedACL  = S3CannedACL.PublicRead
                    };

                    PutObjectResponse response1 = await client.PutObjectAsync(putRequest1);

                    return("https://s3-ap-southeast-2.amazonaws.com/openchargemap/images/" + blobName);

                    /* // 2. Put object-set ContentType and add metadata.
                     * PutObjectRequest putRequest2 = new PutObjectRequest
                     * {
                     *   BucketName = bucketName,
                     *   Key = keyName,
                     *   FilePath = filePath,
                     *   ContentType = "text/plain"
                     * };
                     * putRequest2.Metadata.Add("x-amz-meta-title", "someTitle");
                     *
                     * PutObjectResponse response2 = client.PutObject(putRequest2);*/
                }
                catch (AmazonS3Exception amazonS3Exception)
                {
                    if (amazonS3Exception.ErrorCode != null &&
                        (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                         ||
                         amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                    {
                        Console.WriteLine("Check the provided AWS Credentials.");
                        Console.WriteLine(
                            "For service sign up go to http://aws.amazon.com/s3");
                    }
                    else
                    {
                        Console.WriteLine(
                            "Error occurred. Message:'{0}' when writing an object"
                            , amazonS3Exception.Message);
                    }
                    return(null);
                }
            }
        }
示例#40
0
    static void Main(string[] args)
    {
        var client = new Amazon.S3.AmazonS3Client();

        using (var ms = new MemoryStream())     // Load the data into memorystream from a data source other than a file
        {
            using (var transferUtility = new TransferUtility(client))
            {
                transferUtility.Upload(ms, "bucket", "key");
            }
        }
    }
        private void EditFile(HttpChallenge httpChallenge, bool delete, TextWriter msg)
        {
            var filePath = httpChallenge.FilePath;

            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            if (filePath.StartsWith("/"))
            {
                filePath = filePath.Substring(1);
            }

            using (var s3 = new Amazon.S3.AmazonS3Client(
                       CommonParams.ResolveCredentials(),
                       CommonParams.RegionEndpoint))
            {
                if (delete)
                {
                    LOG.Debug("Deleting S3 object at Bucket [{0}] and Key [{1}]", BucketName, filePath);
                    var s3Requ = new Amazon.S3.Model.DeleteObjectRequest
                    {
                        BucketName = BucketName,
                        Key        = filePath,
                    };
                    var s3Resp = s3.DeleteObject(s3Requ);
                    if (LOG.IsDebugEnabled)
                    {
                        LOG.Debug("Delete response: [{0}]",
                                  NLog.Targets.DefaultJsonSerializer.Instance.SerializeObject(s3Resp));
                    }

                    msg.WriteLine("* Challenge Response has been deleted from S3");
                    msg.WriteLine("    at Bucket/Key: [{0}/{1}]", BucketName, filePath);
                }
                else
                {
                    var s3Requ = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName  = BucketName,
                        Key         = filePath,
                        ContentBody = httpChallenge.FileContent,
                        ContentType = ContentType,
                        CannedACL   = S3CannedAcl,
                    };
                    var s3Resp = s3.PutObject(s3Requ);

                    msg.WriteLine("* Challenge Response has been written to S3");
                    msg.WriteLine("    at Bucket/Key: [{0}/{1}]", BucketName, filePath);
                    msg.WriteLine("* Challenge Response should be accessible with a MIME type of [text/json]");
                    msg.WriteLine("    at: [{0}]", httpChallenge.FileUrl);
                }
            }
        }
示例#42
0
        public void Delete(string key)
        {
            Amazon.S3.AmazonS3Client client = getS3Client();
            var resTask = client.DeleteObjectAsync(new Amazon.S3.Model.DeleteObjectRequest()
            {
                BucketName = bucketName,
                Key        = key
            });

            resTask.Wait();
            if (resTask.Result.HttpStatusCode != System.Net.HttpStatusCode.OK &&
                resTask.Result.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
            {
                throw new Exception("Delete failed, error code: " + resTask.Result.HttpStatusCode, resTask.Exception);
            }
        }
示例#43
0
        private void uploadContent(String content, String bucket, String key)
        {
            using (var s3client = new Amazon.S3.AmazonS3Client())
            {
                var uploadPromise = s3client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
                {
                    BucketName  = bucket,
                    Key         = key,
                    ContentBody = content
                });

                uploadPromise.Wait();

                Console.WriteLine($"Uploaded s3://{bucket}/{key} ETAG {uploadPromise.Result.ETag}");
            }
        }
示例#44
0
        public void TestSessionCredentials()
        {
            using (var sts = new Amazon.SecurityToken.AmazonSecurityTokenServiceClient())
            {
                AWSCredentials credentials = sts.GetSessionToken().Credentials;

                var originalS3Signature = AWSConfigsS3.UseSignatureVersion4;
                AWSConfigsS3.UseSignatureVersion4 = true;
                try
                {
                    using (var ec2 = new Amazon.EC2.AmazonEC2Client(credentials))
                    {
                        var regions = ec2.DescribeRegions().Regions;
                        Console.WriteLine(regions.Count);
                    }

                    using (var s3 = new Amazon.S3.AmazonS3Client(credentials))
                    {
                        var buckets = s3.ListBuckets().Buckets;
                        Console.WriteLine(buckets.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest {
                            RegistrationStatus = "REGISTERED"
                        }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }


                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials, new Amazon.SimpleWorkflow.AmazonSimpleWorkflowConfig {
                        SignatureVersion = "4"
                    }))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest {
                            RegistrationStatus = "REGISTERED"
                        }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }
                }
                finally
                {
                    AWSConfigsS3.UseSignatureVersion4 = originalS3Signature;
                }
            }
        }
示例#45
0
        public static IAlohaBuilder AddAmazonS3(this IAlohaBuilder builder, string sectionName = SectionName)
        {
            var options = builder.GetOptions <AmazonS3Options>(sectionName);

            builder.Services.AddSingleton(options);

            IAmazonS3 amazonS3 = new Amazon.S3.AmazonS3Client(
                new BasicAWSCredentials(options.AccessKey, options.SecretKey),
                new AmazonS3Config {
                ServiceURL = options.ServiceUrl
            });

            builder.Services.AddSingleton(amazonS3);

            builder.Services.AddSingleton <IStorageClient, AmazonS3Client>();

            return(builder);
        }
示例#46
0
        public override async Task DoAction(string RoleARN)
        {
            await base.DoAction(RoleARN);

            var logger = LogManager.GetCurrentClassLogger();


            Parallel.ForEach(SharedLibrary.Utilities.GetRegions(), (region) =>
            {
                logger.Debug($"Checking S3 buckets in region {region.DisplayName }");

                var creds = SharedLibrary.Utilities.AssumeRole(RoleARN, region);

                Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(creds, region);

                var listBucketsResult = client.ListBucketsAsync(new ListBucketsRequest {
                }).Result;

                foreach (var bucket in listBucketsResult.Buckets)
                {
                    try
                    {
                        var bucketLocationResult = client.GetBucketLocationAsync(new GetBucketLocationRequest {
                            BucketName = bucket.BucketName
                        }).Result;

                        var bucketRegion = RegionEndpoint.GetBySystemName(bucketLocationResult.Location.Value);

                        Amazon.S3.AmazonS3Client localClient = new Amazon.S3.AmazonS3Client(creds, bucketRegion);

                        EmptyBucket(localClient, bucket.BucketName).Wait();

                        var deleteBucketResult = localClient.DeleteBucketAsync(new DeleteBucketRequest {
                            BucketName = bucket.BucketName, BucketRegion = bucketLocationResult.Location
                        }).Result;


                        logger.Debug($"Bucket {bucket.BucketName} in region {region.DisplayName} deleted.");
                    }
                    catch
                    { }
                }
            });
        }
示例#47
0
        public async Task EditFile(string filePath, string contentType, string content)
        {
#pragma warning disable 618 // "'StoredProfileCredentials' is obsolete..."
            var creds = new StoredProfileAWSCredentials("acmesharp-tests");
#pragma warning restore 618
            var reg    = RegionEndpoint.GetBySystemName(AwsRegion);
            var delete = content == null;

            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            // if (filePath.StartsWith("/"))
            //     filePath = filePath.Substring(1);
            filePath = filePath.Trim('/');

            using (var s3 = new Amazon.S3.AmazonS3Client(creds, reg))
            {
                if (delete)
                {
                    var s3Requ = new Amazon.S3.Model.DeleteObjectRequest
                    {
                        BucketName = BucketName,
                        Key        = filePath,
                    };
                    var s3Resp = await s3.DeleteObjectAsync(s3Requ);
                }
                else
                {
                    using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                    {
                        var s3Requ = new Amazon.S3.Model.PutObjectRequest
                        {
                            BucketName  = BucketName,
                            Key         = filePath,
                            ContentType = contentType,
                            InputStream = ms,
                            CannedACL   = S3CannedAcl,
                        };

                        var s3Resp = await s3.PutObjectAsync(s3Requ);
                    }
                }
            }
        }
示例#48
0
        public async Task <IActionResult> Put(int id, [FromForm] string nome, [FromForm] string cpf, [FromForm] string usuario, [FromForm] string senha, IFormFile file)
        {
            var memoryStream = new MemoryStream();

            file.CopyTo(memoryStream);

            var request = new PutObjectRequest
            {
                BucketName  = "mega-hacka",
                Key         = file.FileName,
                InputStream = memoryStream,
                ContentType = file.ContentType,
                CannedACL   = S3CannedACL.PublicRead
            };

            var s3Client = new Amazon.S3.AmazonS3Client(Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"),
                                                        Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY"),
                                                        Amazon.RegionEndpoint.USEast1);

            var response = await s3Client.PutObjectAsync(request);

            var obj = await _context.Cliente.FindAsync(id);

            if (obj == null)
            {
                return(NotFound());
            }

            obj.CPF       = cpf;
            obj.Nome      = nome;
            obj.Usuario   = usuario;
            obj.Senha     = senha;
            obj.UrlImagem = $"https://mega-hacka.s3.amazonaws.com/{request.Key}";

            _context.Entry(obj).State = EntityState.Modified;
            await _context.SaveChangesAsync();


            return(Ok());
        }
示例#49
0
        private void EditFile(HttpChallenge httpChallenge, bool delete)
        {
            var filePath = httpChallenge.FilePath;

            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            if (filePath.StartsWith("/"))
            {
                filePath = filePath.Substring(1);
            }

            using (var s3 = new Amazon.S3.AmazonS3Client(
                       CommonParams.ResolveCredentials(),
                       CommonParams.RegionEndpoint))
            {
                if (delete)
                {
                    var s3Requ = new Amazon.S3.Model.DeleteObjectRequest
                    {
                        BucketName = BucketName,
                        Key        = filePath,
                    };
                    var s3Resp = s3.DeleteObject(s3Requ);
                }
                else
                {
                    var s3Requ = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName  = BucketName,
                        Key         = filePath,
                        ContentBody = httpChallenge.FileContent,
                        ContentType = ContentType,
                        CannedACL   = S3CannedAcl,
                    };
                    var s3Resp = s3.PutObject(s3Requ);
                }
            }
        }
示例#50
0
        public void ObtenerRutaAdjunto()
        {
            string regionAws  = "us-west-2";
            string usuarioAws = "CondaiUser";
            string accessKey  = "AKIAIB7EDHV2CPLYDT4A";
            string secretKey  = "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";

            Amazon.Util.ProfileManager.RegisterProfile(usuarioAws, accessKey, secretKey);

            Amazon.RegionEndpoint         region      = Amazon.RegionEndpoint.GetBySystemName(regionAws);
            Amazon.Runtime.AWSCredentials credentials = new Amazon.Runtime.StoredProfileAWSCredentials(usuarioAws);

            Amazon.S3.IAmazonS3 s3Client = new Amazon.S3.AmazonS3Client(credentials, region);

            GetPreSignedUrlRequest request = new GetPreSignedUrlRequest()
            {
                BucketName = "abccondai",
                Key        = "Arena.jpg",
                Expires    = DateTime.Now.AddSeconds(60)
            };

            var a = s3Client.GetPreSignedURL(request);
        }
示例#51
0
        public static void UploadFileToAmazonS3(FileStream fu, string bucketName, string fileName)
        {
            if (fu != null)
            {
                if (FileExistsInAmazonS3(System.Configuration.ConfigurationSettings.AppSettings["AWSBucket_Small"], fileName))
                {
                    DeleteFileInAmazonS3(System.Configuration.ConfigurationSettings.AppSettings["AWSBucket_Small"], fileName);
                }

                Amazon.S3.AmazonS3Client s3Client = new Amazon.S3.AmazonS3Client();

                PutObjectRequest request = new PutObjectRequest();
                request.InputStream = fu;
                request.BucketName  = bucketName;
                request.CannedACL   = S3CannedACL.PublicRead;
                request.Key         = fileName;
                s3Client.PutObject(request);
            }
            else
            {
                throw new System.Exception("File Empty.");
            }
        }
示例#52
0
        private static void UploadFile(aws_class_data aws_data)
        {
            using (var client = new Amazon.S3.AmazonS3Client(aws_data.AwsAccessKey, aws_data.AwsSecretKey, Amazon.RegionEndpoint.EUCentral1))
            {
                var fs = new FileStream(aws_data.FileToUpload, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                try
                {
                    var request = new PutObjectRequest();
                    request.BucketName  = aws_data.AwsS3BucketName + "/" + aws_data.AwsS3FolderName;
                    request.CannedACL   = S3CannedACL.Private;
                    request.Key         = Path.GetFileName(aws_data.FileToUpload);
                    request.InputStream = fs;
                    client.PutObject(request);
                }
                catch (AmazonS3Exception ex)
                {
                    ErrorLog.ErrorLog.toErrorFile(ex.GetBaseException().ToString());
                    if (string.IsNullOrEmpty(ex.ErrorCode))
                    {
                        ErrorLog.ErrorLog.toErrorFile("Amazon error code: {0}" + "None");
                    }
                    else
                    {
                        ErrorLog.ErrorLog.toErrorFile("Amazon error code: {0}" + ex.ErrorCode);
                    }

                    //Console.WriteLine("Amazon error code: {0}", string.IsNullOrEmpty(ex.ErrorCode) ? "None" : ex.ErrorCode);
                    ErrorLog.ErrorLog.toErrorFile("Exception message: {0}" + ex.Message);
                }
                catch (Exception ex)
                {
                    ErrorLog.ErrorLog.toErrorFile(ex.GetBaseException().ToString());
                    ErrorLog.ErrorLog.toErrorFile("Exception message: {0}" + ex.Message);
                }
            }
        }
示例#53
0
        public static GetObjectResponse GetFile(AwsCommonParams commonParams,
                                                string bucketName, string filePath)
        {
            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            // This also implements behavior consistent with the
            // edit counterpart routine for verification purposes
            if (filePath.StartsWith("/"))
            {
                filePath = filePath.Substring(1);
            }

            using (var s3 = new Amazon.S3.AmazonS3Client(
                       commonParams.ResolveCredentials(),
                       commonParams.RegionEndpoint))
            {
                var s3Requ = new Amazon.S3.Model.GetObjectRequest
                {
                    BucketName = bucketName,
                    //Prefix = filePath,
                    Key = filePath,
                };

                //var s3Resp = s3.ListObjects(s3Requ);
                try
                {
                    var s3Resp = s3.GetObject(s3Requ);

                    return(s3Resp);
                }
                catch (AmazonS3Exception ex)
                    when(ex.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(null);
                    }
            }
        }
示例#54
0
        public async Task <IActionResult> Post([FromForm] string nome, [FromForm] string cpf, [FromForm] string usuario, [FromForm] string senha, IFormFile file)
        {
            var memoryStream = new MemoryStream();

            file.CopyTo(memoryStream);

            var request = new PutObjectRequest
            {
                BucketName  = "mega-hacka",
                Key         = file.FileName,
                InputStream = memoryStream,
                ContentType = file.ContentType,
                CannedACL   = S3CannedACL.PublicRead
            };



            var s3Client = new Amazon.S3.AmazonS3Client(Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"),
                                                        Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY"),
                                                        Amazon.RegionEndpoint.USEast1);

            var response = await s3Client.PutObjectAsync(request);

            var cliente = new Cliente();

            cliente.Nome      = nome;
            cliente.CPF       = cpf;
            cliente.Usuario   = usuario;
            cliente.Senha     = senha;
            cliente.UrlImagem = $"https://mega-hacka.s3.amazonaws.com/{request.Key}";

            await _context.Cliente.AddAsync(cliente);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", new { id = cliente.Id }));
        }
示例#55
0
        async Task EmptyBucket(Amazon.S3.AmazonS3Client client, string BucketName)
        {
            int itemCount = 0;
            var logger    = LogManager.GetCurrentClassLogger();

            do
            {
                var listObjectsResult = await client.ListObjectsV2Async(new ListObjectsV2Request { BucketName = BucketName });

                itemCount = listObjectsResult.KeyCount;

                if (itemCount > 0)
                {
                    var deleteObjectsResult = await client.DeleteObjectsAsync(new DeleteObjectsRequest { BucketName = BucketName, Objects = listObjectsResult.S3Objects.Select(a => new KeyVersion {
                            Key = a.Key, VersionId = null
                        }).ToList() });

                    if (deleteObjectsResult.HttpStatusCode == System.Net.HttpStatusCode.OK)
                    {
                        logger.Debug($"Successfully deleted {deleteObjectsResult.DeletedObjects.Count} objects from bucket {BucketName}.");
                    }
                }
            } while (itemCount > 0);
        }
示例#56
0
    public Dictionary <string, string> Configure(ISynapseDalConfig conifg)
    {
        if (conifg != null)
        {
            string         s    = YamlHelpers.Serialize(conifg.Config);
            AwsS3DalConfig fsds = YamlHelpers.Deserialize <AwsS3DalConfig>(s);

            _region = Amazon.RegionEndpoint.USEast1.ToString();

            if (string.IsNullOrWhiteSpace(fsds.AwsAccessKey) || string.IsNullOrWhiteSpace(fsds.AwsSecretAccessKey))
            {
                _awsClient = new zf.AwsClient(Amazon.RegionEndpoint.USEast1);
                _s3Client  = new Amazon.S3.AmazonS3Client(Amazon.RegionEndpoint.USEast1);
            }
            else
            {
                _awsClient = new zf.AwsClient(fsds.AwsAccessKey, fsds.AwsSecretAccessKey, Amazon.RegionEndpoint.USEast1);
                _s3Client  = new Amazon.S3.AmazonS3Client(fsds.AwsAccessKey, fsds.AwsSecretAccessKey, Amazon.RegionEndpoint.USEast1);
            }

            _bucketName = fsds.DefaultBucketName;

            _planPath            = fsds.PlanFolderPath;
            _histPath            = fsds.HistoryFolderPath;
            _histAsJson          = fsds.WriteHistoryAs == HistorySerializationFormat.FormattedJson || fsds.WriteHistoryAs == HistorySerializationFormat.CompressedJson;
            _histAsFormattedJson = fsds.WriteHistoryAs == HistorySerializationFormat.FormattedJson;
            _histExt             = _histAsJson ? ".json" : ".yaml";
            _splxPath            = fsds.Security.FilePath;

            EnsurePaths();

            ProcessPlansOnSingleton   = fsds.ProcessPlansOnSingleton;
            ProcessActionsOnSingleton = fsds.ProcessActionsOnSingleton;

            LoadSuplex();

            if (_splxDal == null && fsds.Security.IsRequired)
            {
                throw new Exception($"Security is required.  Could not load security file: {_splxPath}.");
            }

            if (_splxDal != null)
            {
                _splxDal.LdapRoot = conifg.LdapRoot;
                _splxDal.GlobalExternalGroupsCsv = fsds.Security.GlobalExternalGroupsCsv;
            }
        }
        else
        {
            ConfigureDefaults();
        }

        string name = nameof(AwsS3Dal);
        Dictionary <string, string> props = new Dictionary <string, string>
        {
            { name, CurrentPath },
            { $"{name} AWS Region", _region },
            { $"{name} S3 Default Bucket", _bucketName },
            { $"{name} Plan path", _planPath },
            { $"{name} History path", _histPath },
            { $"{name} Security path", _splxPath }
        };

        return(props);
    }
 public AmazonS3ClientService(IOptions <AmazonS3ClientOptions> options, ILogger <AmazonS3ClientService> logger)
 {
     Options   = options.Value;
     _logger   = logger;
     _s3Client = new Amazon.S3.AmazonS3Client(Options.AccessKeyId, Options.SecretKey, Options.AmazonS3Config);
 }
示例#58
0
 public AmazonS3Storage(ILogger logger, AmazonS3Config config)
 {
     _logger = logger;
     _client = new Amazon.S3.AmazonS3Client(config);
 }
示例#59
0
 public AmazonS3Storage(ILogger logger, AmazonS3Config config, string accessKey, string accessSecret)
 {
     _logger = logger;
     _client = new Amazon.S3.AmazonS3Client(accessKey, accessSecret, config);
 }
示例#60
-1
        public async Task DownloadTestFile()
        {
            var token = await S3Service.GetS3Token("upload");

                string filePath = "c:/temp/download/test.jpg";
                string awsAccessKeyId = token.accessKeyId;
                string awsSecretAccessKey = token.secretAccessKey;
                string awsSessionToken = token.sessionToken;
                string existingBucketName = token.bucket;
                string keyName = string.Format("{0}/{1}", token.path, Path.GetFileName(filePath));

                var client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, awsSessionToken, RegionEndpoint.APSoutheast2);
                var fileTransferUtility = new TransferUtility(client);

                var request = new TransferUtilityDownloadRequest()
                {
                    BucketName = existingBucketName,
                    FilePath = filePath,
                    Key = keyName,
                    ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = token.uploadPassword,
                };

                fileTransferUtility.Download(request);
                Console.WriteLine("download 1 completed");
        }