private static string MoveToS3(AmazonS3Client s3Client, string bucket, int?chunkId, int?subChunkId, IDataReader reader, string tableName) { var folder = tableName; string fileName; if (chunkId.HasValue) { var indexOfSet = ServicesManager.GetIndexOfSet(chunkId.Value); fileName = string.Format(@"{0}/{1}", indexOfSet, tableName + ".txt.gz." + chunkId + "." + subChunkId); } else { fileName = string.Format(@"{0}", folder + ".txt.gz"); } if (!(AmazonS3Util.DoesS3BucketExist(s3Client, Settings.Current.Bucket))) { AmazonS3Helper.CreateBucket(s3Client, Settings.Current.Bucket); } AmazonS3Helper.CopyFile(s3Client, bucket, fileName, reader, tableName); return(fileName); }
public string CreateBucket() { NameValueCollection appSettings = ConfigurationManager.AppSettings; using (AWSS3.client = new AmazonS3Client()) { try { if (!AmazonS3Util.DoesS3BucketExist(AWSS3.client, appSettings["BucketName"])) { PutBucketRequest request = new PutBucketRequest { BucketName = appSettings["BucketName"], UseClientRegion = true }; AWSS3.client.PutBucket(request); } } catch (AmazonS3Exception ex) { if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.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", ex.Message); } } } return(appSettings["BucketName"]); }
internal virtual bool BucketExists(string bucketName) { using (var client = DefaultAmazonClient()) { return(AmazonS3Util.DoesS3BucketExist(client, bucketName)); } }
internal void StartUpload() { var amazonCon = Connection as AmazonS3Connection; if (amazonCon != null) { var amazonClient = new AmazonS3Client(amazonCon.AccessKey, amazonCon.SecretAccessKey, amazonCon.GetRegion()); fileTransferUtility = new TransferUtility(amazonClient); if (!(AmazonS3Util.DoesS3BucketExist(amazonClient, amazonCon.BucketName))) { CreateABucket(amazonClient, amazonCon.BucketName); } var uploadRequest = new TransferUtilityUploadRequest { BucketName = amazonCon.BucketName, FilePath = FullPath, CannedACL = S3CannedACL.PublicRead, }; uploadRequest.UploadProgressEvent += uploadRequest_UploadPartProgressEvent; fileTransferUtility.UploadAsync(uploadRequest); Trace.WriteLine("Start Upload : " + FullPath); } }
/// <summary> /// Ensure the specified bucket exists. /// </summary> /// <param name="bucketName">The name of the bucket.</param> public void EnsureBucketExists(string bucketName) { if (!AmazonS3Util.DoesS3BucketExist(bucketName, _amazonS3)) { CreateBucket(bucketName); } }
private void CreateBucketIfNeeded(string bucketName) { if (!AmazonS3Util.DoesS3BucketExist(bucketName, Client)) { CreateBucket(bucketName); } }
public ActionResult S3Test() { string bucketName = "s3test"; string keyName = "Data.txt"; using (var client = new AmazonS3Client("<your-access-key>", "<your-secret-key>", new AmazonS3Config { ServiceURL = "<your-minio-hostname>", SignatureVersion = "2", ForcePathStyle = true })) { //Create the bucket if it isn't there if (!(AmazonS3Util.DoesS3BucketExist(client, bucketName))) { CreateABucket(client, bucketName); } //Shove the current date/time into a key called "Data.txt" client.PutObject(new PutObjectRequest { BucketName = bucketName, Key = keyName, ContentBody = DateTime.Now.ToString() }); using (GetObjectResponse response = client.GetObject(bucketName, keyName)) { using (StreamReader reader = new StreamReader(response.ResponseStream)) { ViewBag.Message = reader.ReadToEnd(); } } } return(View()); }
//Métodos que controlam o envio de arquivos para o Amazon Simple Storage Service (S3) internal static bool SendObject(Licitacao l, string pathEditais, string fileName) { bool segundaTentativa = false; try { do { //cria o bucket no cliente caso o bucket não exista if (!AmazonS3Util.DoesS3BucketExist(s3Client, bucketName)) { CreateABucket(s3Client); } //Reúne os dados necessários do arquivo PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName + "/licitacoes", Key = fileName, FilePath = pathEditais + fileName, Grants = new List <S3Grant> { new S3Grant { Grantee = new S3Grantee { URI = "http://acs.amazonaws.com/groups/global/AllUsers" }, Permission = S3Permission.READ } }/*, * TagSet = new List<Tag> * { * new Tag {Key = "IdLicitacao", Value = l.Id.ToString() }, * new Tag {Key = "Lote", Value = l.Lote.Id.ToString() }, * new Tag {Key = "Fonte", Value = l.LinkSite } * }*/ }; //Envia o arquivo para o bucket PutObjectResponse putResponse = s3Client.PutObject(putRequest); return(true); } while (segundaTentativa); } catch (Exception e) { RService.Log("RService Exception AWS (SendObject): " + e.Message + " / " + e.StackTrace + " / " + e.InnerException + " / " + e.InnerException + " / " + e.InnerException + " at {0}", Path.GetTempPath() + "RSERVICE" + ".txt"); //Faz uma segunda tentativa de envio segundaTentativa = !segundaTentativa; if (segundaTentativa) { RService.Log("AWS (SendObject): Segunda tentativa de envio... at {0}", Path.GetTempPath() + "RSERVICE" + ".txt"); Thread.Sleep(5000); } else { return(false); } } return(false); }
private static void CheckBucket() { Amazon.Runtime.AWSCredentials credentials = new Amazon.Runtime.BasicAWSCredentials(awsAccessKey, awsSecretKey); using (var client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.EUCentral1)) { if (!(AmazonS3Util.DoesS3BucketExist(client, bucketName))) { CreateBucket(client); } UploadFiles(client); } }
//public AmazonStorage() //{ // AmazonS3Client client = new AmazonS3Client(); //} public AmazonStorage(string _bucket, string _username) { using (var client = new AmazonS3Client()) { bucket = _bucket; username = _username; if (!(AmazonS3Util.DoesS3BucketExist(client, bucketName))) { CreateABucket(client); } // Retrieve bucket location. string bucketLocation = FindBucketLocation(client); } }
public void HappyCaseDoesS3BucketExist() { // make sure the cache works when it gets the region from the x-amz-bucket-region header using (var runner = new BucketRegionTestRunner(true, true)) { if (runner.TestBucketIsReady) { Assert.IsTrue(AmazonS3Util.DoesS3BucketExist(runner.USEast1Client, runner.BucketName)); RegionEndpoint cachedRegion; Assert.IsTrue(BucketRegionDetector.BucketRegionCache.TryGetValue(runner.BucketName, out cachedRegion)); Assert.AreEqual(RegionEndpoint.USWest1, cachedRegion); } } }
public void createBucket() { using (var client = new AmazonS3Client(_accessKey, _secretKey, Amazon.RegionEndpoint.EUWest1)) { if (!(AmazonS3Util.DoesS3BucketExist(client, bucketName))) { CreateABucket(client); } // Retrieve bucket location. string bucketLocation = FindBucketLocation(client); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
public bool bDoesBucketExist() { bool bReturn = false; try { if (AmazonS3Util.DoesS3BucketExist(client, this.strBucketName)) { bReturn = true; } } catch (Exception ex) { throw new Exception("Error bDoesBucketExist() " + ex.Message + "..."); } return(bReturn); }
public void CreateBucket() { if (AmazonS3Util.DoesS3BucketExist(client, bucketName)) { Console.WriteLine("Bucket already exists"); } else { var bucket = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; var bucketResponsoe = client.PutBucket(bucket); if (bucketResponsoe.HttpStatusCode.IsSuccess()) { Console.WriteLine("Bucket Created Successfully"); } } }
public static string CreateABucket() { string result = "1"; try { using (var client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKeyId, Amazon.RegionEndpoint.USEast1)) { if (!(AmazonS3Util.DoesS3BucketExist(client, bucketName))) { //CreateABucket(client); PutBucketRequest putRequest1 = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; PutBucketResponse response1 = client.PutBucket(putRequest1); result = response1.HttpStatusCode.ToString().ToLower(); // ok } else { // Retrieve bucket location. //string bucketLocation = FindBucketLocation(client); result = "alreadyexist"; } } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { result = "Check the provided AWS Credentials."; //Console.WriteLine("Check the provided AWS Credentials."); //Console.WriteLine("For service sign up go to http://aws.amazon.com/s3"); } else { result = "Error occurred. Message:" + amazonS3Exception.Message + " when writing an object;"; } } return(result); }
public void Test() { const string bucketName = "sample_bucket"; const string key = "sample"; using (var client = Client()) { if (!AmazonS3Util.DoesS3BucketExist(client, bucketName)) { var putRequest1 = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; client.PutBucket(putRequest1); } const string objectBody = "test sample"; var putRequest = new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = objectBody }; client.PutObject(putRequest); var request = new GetObjectRequest { BucketName = bucketName, Key = key }; using (var response = client.GetObject(request)) using (var responseStream = response.ResponseStream) using (var reader = new StreamReader(responseStream)) { var body = reader.ReadToEnd(); Assert.That(body, Is.EqualTo(objectBody)); } } }
public void CreateBucket() { if (AmazonS3Util.DoesS3BucketExist(client, "myapp1238521478")) { Console.WriteLine("Bucket Already exist"); } else { var bucketRequest = new PutBucketRequest { BucketName = "myapp1238521478", UseClientRegion = true }; var bucketResponse = client.PutBucket(bucketRequest); if (bucketResponse.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("Crated bucket successfully"); } } }
private void btn_upload_Click(object sender, EventArgs e) { int i = 0; foreach (DataGridViewRow row in dgv_fileList.Rows) { if (dgv_fileList.Rows[i].Cells["Type"].Value.ToString() == "Folder") { string itemToUpload = dgv_fileList.Rows[i].Cells["Item"].Value.ToString(); if (!(AmazonS3Util.DoesS3BucketExist(s3Client, itemToUpload))) { DialogResult dialogResult = MessageBox.Show("S3 Bucket does not exist. Would you like to create it?", "Warning", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { CreateBucket(s3Client, itemToUpload); } } } if (dgv_fileList.Rows[i].Cells["Type"].Value.ToString() == "File") { try { TransferUtility fileTransferUtility = new TransferUtility(s3Client); string itemToUpload = dgv_fileList.Rows[i].Cells["Item"].Value.ToString(); string key = itemToUpload.Replace("C:\\", ""); TransferUtilityUploadRequest request = new TransferUtilityUploadRequest { BucketName = "srpdesign", FilePath = itemToUpload, Key = (key.Replace("\\", "/")) }; fileTransferUtility.Upload(request); } catch (AmazonS3Exception ae) { Console.WriteLine(ae.Message, ae.InnerException); } } i++; } try { TransferUtility fileTransferUtility = new TransferUtility(s3Client); string itemToUpload = dgv_fileList.Rows[0].Cells[0].Value.ToString(); string s = itemToUpload.Replace(@"C:\", ""); TransferUtilityUploadRequest request = new TransferUtilityUploadRequest { BucketName = "srpdesign", FilePath = itemToUpload, Key = (s.Replace("\\", "/")) }; fileTransferUtility.Upload(request); } catch (AmazonS3Exception ae) { Console.WriteLine(ae.Message, ae.InnerException); } }
private bool S3_DoesBucketExist(AmazonS3Client client, string bucketName) { return(AmazonS3Util.DoesS3BucketExist(client, bucketName)); }
public static void WaitForBucket(IAmazonS3 client, string bucketName, int maxSeconds) { var sleeper = new UtilityMethods.ListSleeper(500, 1000, 2000, 5000); UtilityMethods.WaitUntil(() => { return(AmazonS3Util.DoesS3BucketExist(client, bucketName)); }, sleeper, 30); }
private void ValidateS3Connection(S3StorageProviderSettingsPart part, IUpdateModel updater) { var provider = _storageProvider as S3StorageProvider; if (provider == null) { return; } IAmazonS3 client = null; try { if (part.UseCustomCredentials) { bool valid = true; if (string.IsNullOrWhiteSpace(part.AWSAccessKey)) { updater.AddModelError("AWSAccessKey", T("Specify a value for AWS Access Key")); valid = false; } if (string.IsNullOrWhiteSpace(part.AWSSecretKey)) { updater.AddModelError("AWSAccessKey", T("Specify a value for AWS Secret Key")); valid = false; } if (string.IsNullOrWhiteSpace(part.RegionEndpoint)) { updater.AddModelError("AWSAccessKey", T("Specify a value for S3 Region Endpoint")); valid = false; } if (!valid) { return; } client = provider.CreateClientFromCustomCredentials(part.AWSAccessKey, part.AWSSecretKey, Amazon.RegionEndpoint.GetBySystemName(part.RegionEndpoint)); if (client != null) { _notifier.Information(T("Connecting using custom credentials: OK")); } } else { var iamCredentials = provider.GetIAMCredentials(); client = provider.CreateClientFromIAMCredentials(iamCredentials); if (client != null) { _notifier.Information(T("Connecting using IAM role: OK")); } } // Check AWS credentials, bucket name and bucket permissions string bucketName = part.Record.BucketName; GetPreSignedUrlRequest request = new GetPreSignedUrlRequest(); request.BucketName = bucketName; if (AWSConfigs.S3Config.UseSignatureVersion4) { request.Expires = DateTime.Now.AddDays(6); } else { request.Expires = new DateTime(2019, 12, 31); } request.Verb = HttpVerb.HEAD; request.Protocol = Protocol.HTTP; string url = client.GetPreSignedURL(request); Uri uri = new Uri(url); if (!AmazonS3Util.DoesS3BucketExist(client, bucketName)) { updater.AddModelError("Settings", T("Invalid bucket name. No bucket by the name {0} exists.", part.Record.BucketName)); } else { // Check for read/write permissions var acl = client.GetACL(new GetACLRequest() { BucketName = bucketName }); var grants = acl.AccessControlList.Grants; if (!grants.Any(x => x.Permission == S3Permission.FULL_CONTROL)) { if (!grants.Any(x => x.Permission == S3Permission.WRITE)) { updater.AddModelError("Settings", T("You don't have write access to this bucket")); } if (!grants.Any(x => x.Permission == S3Permission.READ)) { updater.AddModelError("Settings", T("You don't have read access to this bucket")); } } } _notifier.Information(T("All settings look okay")); } catch (AmazonS3Exception ex) { if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity"))) { updater.AddModelError("Settings", T("Invalid AWS credentials")); } else if (ex.ErrorCode != null && ex.ErrorCode.Equals("AccessDenied")) { updater.AddModelError("Settings", T("Access denied. You don't have permission to access the bucket '{0}'", part.Record.BucketName)); } else { updater.AddModelError("Settings", T("Unknown error: {0}", ex.Message)); } } finally { if (client != null) { client.Dispose(); } } }