private static void TestCredentialsFile(ImmutableCredentials ic)
        {
            var profileName      = "testProfile";
            var profilesLocation = WriteCreds(profileName, ic);

#pragma warning disable 618
            var creds = new StoredProfileAWSCredentials(profileName, profilesLocation);
#pragma warning restore 618
            var rc = creds.GetCredentials();
            Assert.AreEqual(ic.SecretKey, rc.SecretKey);
            Assert.AreEqual(ic.AccessKey, rc.AccessKey);
            Assert.AreEqual(ic.UseToken, rc.UseToken);
            Assert.AreEqual(ic.Token, rc.Token);

            for (int i = 0; i < 4; i++)
            {
                var shouldHaveToken = (i % 2 == 1);
#pragma warning disable 618
                creds = new StoredProfileAWSCredentials(profileName + i, profilesLocation);
#pragma warning restore 618
                Assert.IsNotNull(creds);

                rc = creds.GetCredentials();
                Assert.IsNotNull(rc.AccessKey);
                Assert.IsNotNull(rc.SecretKey);
                Assert.AreEqual(shouldHaveToken, rc.UseToken);

                if (rc.UseToken)
                {
                    Assert.AreEqual(sessionCreds.AccessKey, rc.AccessKey);
                    Assert.AreEqual(sessionCreds.SecretKey, rc.SecretKey);
                    Assert.AreEqual(sessionCreds.Token, rc.Token);
                }
                else
                {
                    Assert.AreEqual(basicCreds.AccessKey, rc.AccessKey);
                    Assert.AreEqual(basicCreds.SecretKey, rc.SecretKey);
                }
            }
        }
        private static void TestCredentialsFile(ImmutableCredentials ic)
        {
            var profileName      = "testProfile";
            var profilesLocation = WriteCreds(profileName, ic);
            var creds            = new StoredProfileAWSCredentials(profileName, profilesLocation);
            var rc = creds.GetCredentials();

            Assert.AreEqual(ic.SecretKey, rc.SecretKey);
            Assert.AreEqual(ic.AccessKey, rc.AccessKey);
            Assert.AreEqual(ic.UseToken, rc.UseToken);
            Assert.AreEqual(ic.Token, rc.Token);

            for (int i = 0; i < 4; i++)
            {
                creds = new StoredProfileAWSCredentials(profileName + i, profilesLocation);
                Assert.IsNotNull(creds);
                rc = creds.GetCredentials();
                Assert.IsNotNull(rc.AccessKey);
                Assert.IsNotNull(rc.SecretKey);
                var shouldHaveToken = (i % 2 == 1);
                Assert.AreEqual(shouldHaveToken, rc.UseToken);
            }
        }
Beispiel #3
0
        public async Task <List <string> > uploadFileS3([FromForm] IFormFile arquivo)
        {
            //*Setup S3 Client
            //var newRegion = RegionEndpoint.GetBySystemName("us-west-new");


            string profile_name = "localstack";
            var    credentials  = new StoredProfileAWSCredentials(profile_name);

            _logger.LogInformation(credentials.GetCredentials().AccessKey);
            _logger.LogInformation(credentials.GetCredentials().SecretKey);

            var config = new AmazonS3Config {
                ServiceURL = "http://localhost:4566"
            };
            var s3 = new AmazonS3Client(credentials, config);

            // //*List Buckets

            var response = await s3.ListBucketsAsync();

            var ListOfBuckets = response.Buckets.Select(bck => bck.BucketName + " - " + bck.CreationDate).ToList();

            foreach (S3Bucket bucket in response.Buckets)
            {
                _logger.LogInformation(bucket.BucketName);
            }

            var teste = await s3.ListObjectsV2Async(new ListObjectsV2Request()
            {
                BucketName = "fileplace"
            });

            var ListOfObjects = teste.S3Objects.Select(obj => obj.Key).ToList();

            //await s3.PutBucketAsync("Olop");
            // try
            // {


            //     var putRequest1 = new PutObjectRequest
            //     {
            //         BucketName = "fileplace",
            //         Key = "asd",
            //         ContentBody = "sample text"
            //     };

            //     PutObjectResponse response1 = await s3.PutObjectAsync(putRequest1);
            //     _logger.LogInformation(response1.ToString());
            // }
            // catch (AmazonS3Exception e)
            // {
            //     Console.WriteLine(
            //             "AWS - Error encountered ***. Message:'{0}' when writing an object"
            //             , e.Message);
            // }
            // catch (Exception e)
            // {
            //     Console.WriteLine(
            //         "Unknown encountered on server. Message:'{0}' when writing an object"
            //         , e.Message);
            // }


            // //* File Upload
            // var fsz = arquivo.OpenReadStream();
            // //fsz.Position = 0;

            // //* Create a Request
            // var request = new PutObjectRequest()
            // {
            //     BucketName = "fileplace",
            //     Key = arquivo.FileName,
            //     //ContentType = arquivo.ContentType,
            //     //CannedACL = S3CannedACL.PublicRead,
            //     //InputStream = fsz,
            //     ContentBody = "Olá"
            // };


            // var objrsp = await s3.PutObjectAsync(request);
            // var Laresult = string.Format("http://{0}.s3.amazonaws.com/{1}", request.BucketName, request.Key);

            //_logger.LogInformation(Laresult);

            return(ListOfBuckets);
            //return await fileUpload.Upload(arquivo);
            //return this.userServices.createUser(user);
        }