Beispiel #1
0
        //public bool IsFolderExists(string accessKeyID, string secretAccessKey, string bucketName, string key, bool create)
        //{
        //    AmazonS3 client = GetS3Client(accessKeyID, secretAccessKey);
        //    ListObjectsRequest listRequest = new ListObjectsRequest();
        //    listRequest.WithBucketName(bucketName)
        //    .WithPrefix(key);

        //    // get all objects inside the "folder"
        //    ListObjectsResponse objects = client.ListObjects(listRequest);
        //    foreach (S3Object s3o in objects.S3Objects)
        //    {
        //        // get the acl of the object
        //        GetACLRequest aclRequest = new GetACLRequest();
        //        aclRequest.WithBucketName("thebucket")
        //        .WithKey(s3o.Key);
        //        GetACLResponse getAclResponse = client.GetACL(aclRequest);

        //        //// copy the object without acl
        //        //string newKey = s3o.Key.Replace(oldOWnerId.ToString(), newOwnerId.ToString());
        //        //CopyObjectRequest copyRequest = new CopyObjectRequest();
        //        //copyRequest.SourceBucket = "thebucket";
        //        //copyRequest.DestinationBucket = "thebucket";
        //        //copyRequest.WithSourceKey(s3o.Key)
        //        //.WithDestinationKey(newKey);
        //        //S3Response copyResponse = client.CopyObject(copyRequest);
        //        //// set the acl of the newly made object
        //        //SetACLRequest setAclRequest = new SetACLRequest();
        //        //setAclRequest.WithBucketName("ytimusic")
        //        //.WithKey(newKey)
        //        //.WithACL(getAclResponse.AccessControlList);
        //        //SetACLResponse setAclRespone = client.SetACL(setAclRequest);
        //        //DeleteObjectRequest deleteRequest = new DeleteObjectRequest();
        //        //deleteRequest.WithBucketName("thebucket")
        //        //.WithKey(s3o.Key);
        //        //DeleteObjectResponse deleteResponse = client.DeleteObject(deleteRequest);

        //    }

        //    return false;
        //}

        /// <summary>
        /// Check Azure connection
        /// </summary>
        /// <param name="azureAccountName"></param>
        /// <param name="azureAccountKey"></param>
        /// <returns></returns>
        public bool IsBucketNameExists(string accessKeyID, string secretAccessKey, string bucketName, bool create)
        {
            try
            {
                AmazonS3            client   = GetS3Client(accessKeyID, secretAccessKey);
                ListBucketsResponse response = client.ListBuckets();
                bool found = false;
                foreach (S3Bucket bucket in response.Buckets)
                {
                    if (bucket.BucketName == bucketName)
                    {
                        found = true;
                        break;
                    }
                }
                if (found == false)
                {
                    if (create)
                    {
                        client.PutBucket(new PutBucketRequest().WithBucketName(bucketName));
                    }
                    else
                    {
                        throw new Exception("The bucket " + bucketName + " does not exists.");
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                throw new DuradosException("AWS Connection failed, Error: " + ex.Message);
                //return false;
            }
        }
Beispiel #2
0
        private void CreateBucket(AmazonS3 client)
        {
            PutBucketRequest request = new PutBucketRequest();

            request.BucketName = this.BucketName;
            client.PutBucket(request);
        }
Beispiel #3
0
        public bool CreateBucket()
        {
            config.ServiceURL = "s3.amazonaws.com";

            AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                am.PublicKey,
                am.PrivateKey
                );

            ListBucketsResponse response = client.ListBuckets();

            bool found = false;

            foreach (S3Bucket bucket in response.Buckets)
            {
                if (bucket.BucketName == "Drone")
                {
                    found = true;
                    break;
                }
            }

            if (found == false)
            {
                client.PutBucket(new PutBucketRequest().WithBucketName("Drone"));
            }
            return(found);
        }
Beispiel #4
0
        // Parse the input request



        /// <summary>
        /// //////////////////////////////Create a new bucket//////////////////////////////
        /// </summary>
        /// <param name="client"></param>
        public static void CreateBucket(AmazonS3 client, string cmap)
        {
            Console.Out.WriteLine("Checking S3 bucket with name " + cmap);

            ListBucketsResponse response = client.ListBuckets();

            bool found = false;

            foreach (S3Bucket bucket in response.Buckets)
            {
                if (bucket.BucketName == cmap)
                {
                    Console.Out.WriteLine("   Bucket found will not create it.");
                    found = true;
                    break;
                }
            }

            if (found == false)
            {
                Console.Out.WriteLine("   Bucket not found will create it.");

                client.PutBucket(new PutBucketRequest().WithBucketName(cmap));

                Console.Out.WriteLine("Created S3 bucket with name " + cmap);
            }
        }
Beispiel #5
0
        private static void CreateBucket(AmazonS3 s3Client, string bucket)
        {
            var putBucketRequest = new PutBucketRequest {
                BucketName = bucket
            };

            s3Client.PutBucket(putBucketRequest);
        }
Beispiel #6
0
        public void CreateBucket(string bucketName)
        {
            PutBucketRequest request = new PutBucketRequest {
                BucketName = bucketName
            };

            _client.PutBucket(request);
        }
Beispiel #7
0
        /// <summary>
        /// Create a bucket with the specified name.
        /// </summary>
        /// <param name="bucketName">The name of the bucket.</param>
        public void CreateBucket(string bucketName)
        {
            var request = new PutBucketRequest
            {
                BucketName = bucketName
            };

            _amazonS3.PutBucket(request);
        }
Beispiel #8
0
        public bool SaveFile(string sFolder, string sObjectKey, byte[] fileContent, bool bMakePublic)
        {
            try
            {
                AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(S3ACCESSKEY, S3SECRETKEY);
                Amazon.S3.Transfer.TransferUtility uploader = new Amazon.S3.Transfer.TransferUtility(S3ACCESSKEY, S3SECRETKEY);
                string BUCKET_NAME = ConfigurationManager.AppSettings["AWSBUCKET"];

                ListBucketsResponse response = client.ListBuckets();
                bool found = false;
                foreach (S3Bucket bucket in response.Buckets)
                {
                    if (bucket.BucketName == BUCKET_NAME)
                    {
                        found = true;
                        break;
                    }
                }
                if (found == false)
                {
                    client.PutBucket(new PutBucketRequest().WithBucketName(BUCKET_NAME));
                }

                if (sFolder != "")
                {
                    sObjectKey = sFolder + "/" + sObjectKey;
                }

                System.IO.Stream stream = new System.IO.MemoryStream(fileContent);
                Amazon.S3.Transfer.TransferUtilityUploadRequest request = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
                request.WithBucketName(BUCKET_NAME);
                request.WithKey(sObjectKey);
                request.WithInputStream(stream);
                request.WithTimeout(-1);
                if (bMakePublic)
                {
                    request.CannedACL = S3CannedACL.PublicRead;
                }
                uploader.Upload(request);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #9
0
        public static void CleanBucket(string bucket, string aws_id, string aws_secret)
        {
            // set up client
            using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(aws_id, aws_secret))
            {
                // check to ensure that the bucket actually exists
                var dirinfo = new Amazon.S3.IO.S3DirectoryInfo(client, bucket);
                if (dirinfo.Exists)
                {
                    Console.WriteLine("Bucket \"{0}\" already exists.  Erasing.  Sorry if this isn't what you wanted, dude.", bucket);

                    // get a list of the bucket's objects
                    var lor = new ListObjectsRequest
                    {
                        BucketName = bucket
                    };

                    using (ListObjectsResponse r = client.ListObjects(lor))
                    {
                        if (r.S3Objects.Count > 0)
                        {
                            List <KeyVersion> objects = r.S3Objects.Select(obj => new KeyVersion(obj.Key)).ToList();

                            // batch-delete all the objects in the bucket
                            DeleteObjectsRequest dor = new DeleteObjectsRequest
                            {
                                BucketName = bucket,
                                Keys       = objects
                            };
                            client.DeleteObjects(dor);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Creating new bucket \"{0}\"", bucket);

                    // bucket doesn't exist; make a new one
                    PutBucketRequest pbr = new PutBucketRequest
                    {
                        BucketName = bucket
                    };
                    client.PutBucket(pbr);
                }
            }
        }
Beispiel #10
0
 public static void CheckForBucket(string itemKey, AmazonS3 s3Client)
 {
     if (HttpContext.Current.User.Identity.IsAuthenticated)
     {
         string userBucketName = String.Format(Settings.Default.BucketNameFormat, HttpContext.Current.User.Identity.Name, itemKey);
         using (ListBucketsResponse listBucketsResponse = s3Client.ListBuckets())
         {
             S3Bucket bucket = listBucketsResponse.Buckets.FirstOrDefault(b => b.BucketName == userBucketName);
             if (bucket == null)
             {
                 PutBucketRequest putBucketRequest = new PutBucketRequest()
                     .WithBucketName(userBucketName);
                 PutBucketResponse putBucketResponse = s3Client.PutBucket(putBucketRequest);
                 putBucketResponse.Dispose();
             }
         }
     }
 }
Beispiel #11
0
 static void CreateABucket()
 {
     try
     {
         PutBucketRequest request = new PutBucketRequest();
         request.BucketName = bucketName;
         client.PutBucket(request);
     }
     catch (AmazonS3Exception amazonS3Exception)
     {
         if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
         {
             Console.WriteLine("Please check the provided AWS Credentials.");
             Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
         }
         else
         {
             Console.WriteLine("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
         }
     }
 }
        public void TestInitialize()
        {
            try
            {
                this.bucketName = Guid.NewGuid().ToString("N");
                var putRequest = new PutBucketRequest()
                {
                    BucketName = this.bucketName
                };
                using (var putResponse = client.PutBucket(putRequest)) { }

                var objRequest = new PutObjectRequest()
                {
                    BucketName  = this.bucketName,
                    Key         = "sample.txt",
                    ContentBody = "Hello!"
                };
                using (var objResponse = client.PutObject(objRequest)) { }
            }
            catch (Exception e)
            {
                Assert.Inconclusive("prerequisite: unable to create bucket.  Error: {0}", e.Message);
            }
        }
Beispiel #13
0
 public void CreateBucket(string bucketName)
 {
     using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, config))
     {
         try
         {
             PutBucketRequest request = new PutBucketRequest();
             request.WithBucketName(bucketName)
             .WithBucketRegion(S3Region.EU);
             client.PutBucket(request);
         }
         catch (AmazonS3Exception amazonS3Exception)
         {
             if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
             {
                 //log exception - ("Please check the provided AWS Credentials.");
             }
             else
             {
                 //log exception - ("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
             }
         }
     }
 }
Beispiel #14
0
        /// <summary>
        /// Add Bucket
        /// </summary>
        /// <param name="client">Client</param>
        /// <param name="bucket">Bucket</param>
        public void AddBucket(AmazonS3 client, string bucket)
        {
            if (string.IsNullOrWhiteSpace(this.from))
            {
                this.from = bucket;

                this.fromClient = client;
            }
            else
            {
                this.to = bucket;

                this.toClient = client;

                var request = new PutBucketRequest()
                {
                    BucketName = bucket,
                };

                using (var response = client.PutBucket(request))
                {
                }
            }
        }
Beispiel #15
0
        /// <summary>
        /// Add Bucket
        /// </summary>
        /// <param name="client">Client</param>
        /// <param name="bucket">Bucket</param>
        public void AddBucket(AmazonS3 client, string bucket)
        {
            if (string.IsNullOrWhiteSpace(this.from))
            {
                this.from = bucket;

                this.fromClient = client;
            }
            else
            {
                this.to = bucket;

                this.toClient = client;

                var request = new PutBucketRequest()
                {
                    BucketName = bucket,
                };

                using (var response = client.PutBucket(request))
                {
                }
            }
        }
Beispiel #16
0
 private static void CreateBucket(AmazonS3 s3Client, string bucket)
 {
     var putBucketRequest = new PutBucketRequest { BucketName = bucket };
       s3Client.PutBucket(putBucketRequest);
 }
Beispiel #17
0
        private static void CreateBucket(AmazonS3 client, string bucketname)
        {
            Console.Out.WriteLine("Checking S3 bucket with name " + bucketname);

            ListBucketsResponse response = client.ListBuckets();

            bool found = false;
            foreach (S3Bucket bucket in response.Buckets)
            {
                if (bucket.BucketName == bucketname)
                {
                    Console.Out.WriteLine("   Bucket found will not create it.");
                    found = true;
                    break;
                }
            }

            if (found == false)
            {
                Console.Out.WriteLine("   Bucket not found will create it.");

                client.PutBucket(new PutBucketRequest().WithBucketName(bucketname));

                Console.Out.WriteLine("Created S3 bucket with name " + bucketname);
            }
        }
Beispiel #18
0
 private void SetupBucket(AmazonS3 client)
 {
     client.PutBucket(new PutBucketRequest {
         BucketName = BucketName
     });
 }
Beispiel #19
0
 private void SetupBucket(AmazonS3 client)
 {
     client.PutBucket(new PutBucketRequest {BucketName = BucketName});
 }
Beispiel #20
0
        private bool AddImageToAmazon(Coupon coupon, HttpPostedFile File, string ext)
        {
            string count = "";
            string keyname = "";
            //ext = ".jpg";
            //try
            //{
                string accessKeyID = WebConfigurationManager.AppSettings["AWSAccessKey"];
                string secretAccessKeyID = WebConfigurationManager.AppSettings["AWSSecretKey"];

                AmazonS3Config s3Config = new AmazonS3Config();
                s3Config.UseSecureStringForAwsSecretKey = false;

                client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, s3Config);
                //count += "1";
                ICouponDAO couponDAO = _factoryDAO.GetCouponDAO();
                Byte[] imgByte = new byte[0];
                //count += "2";
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //count += "3";
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
                //count += "4";
                //count += ext;
                //count += _merchantname;
                //count += _couponID.ToString();
                //count += File.FileName;
                //count += File.FileName.Substring(0, File.FileName.IndexOf("."));
                //count += ":";
                //count += string.Format("{0}{1}", _couponID.ToString(), ext);
                keyname = string.Format("{0}/{1}", _merchantname, File.FileName.Replace(File.FileName.Substring(0, File.FileName.IndexOf(".")), string.Format("{0}{1}", _couponID.ToString(), ext)));
                //keyname = string.Format("{0}/{1}", _merchantname, File.FileName.Replace(File.FileName.Substring(0, File.FileName.IndexOf(".")), _couponID.ToString()));
                count += keyname;
                /*try
                {
                    //first try deleting old item if applicable
                    DeleteObjectRequest delete_request = new DeleteObjectRequest();
                    delete_request.WithBucketName("gfck").WithKey(string.Format("coupon/{0}", keyname));
                    S3Response delete_response = client.DeleteObject(delete_request);
                    delete_response.Dispose();
                }
                catch (Exception ex)
                {
                    _log.ErrorFormat("Error trying to delete object from bucket: {0} with exception {1}", keyname, ex.Message);
                }*/
                string BUCKET_NAME = String.Format("gfck/coupon/{0}", _merchantname);
                try
                {
                    //add the bucket just in case not already there.
                    //PutObjectRequest request1 = new PutObjectRequest();
                    PutBucketRequest req = new PutBucketRequest();
                    req.WithBucketName(BUCKET_NAME);
                    //count += "6";
                    //request1.WithBucketName("gfck").WithContentType(File.ContentType).WithCannedACL(S3CannedACL.PublicReadWrite).WithKey(BUCKET_NAME);
                    //S3Response response = client.PutObject(request1);
                    //response.Dispose();
                    client.PutBucket(req);
                }
                catch (Exception ex)
                {
                    _log.DebugFormat("This bucket already exists: {0} with exception {1}", BUCKET_NAME, ex.Message);
                }

                //count += "5";
                PutObjectRequest request = new PutObjectRequest();
                //count += "6";
                request.WithBucketName("gfck").WithContentType(File.ContentType).WithCannedACL(S3CannedACL.PublicRead).WithKey(string.Format("coupon/{0}", keyname)).WithInputStream(File.InputStream);
                count += "here";
                S3Response response = client.PutObject(request);
                count += "7";
                response.Dispose();
                count += "8";
                if (ext == "")
                {
                    coupon.Image = keyname;
                }
                else
                {
                    coupon.BottomAdvertisement = keyname;
                }
                count += "9";

                return couponDAO.UpdateCoupon(coupon);
            /*}
            catch (Exception ex)
            {
                _log.ErrorFormat("Exception Occurred: Exception={0}", ex.Message);
                lblError.Text = String.Format("Error with coupon image. {0} {1} {2}", count, keyName, ex.Message);
                lblError.Visible = true;
                lblAddSuccessfull.Visible = false;
                lblEditSuccessfull.Visible = false;
                return false;
            }*/
        }
 private void CreateBucket(AmazonS3 client)
 {
     PutBucketRequest request = new PutBucketRequest();
     request.BucketName = this.BucketName;
     client.PutBucket(request);
 }
        public void SetUp()
        {
            _client = AWSClientFactory.CreateAmazonS3Client(Access_Key_ID, Secret_Access_Key);

            //A pre-requisite for testing S3 Objects. Ensure that we create two temporary buckets to test the Objects.
            //One for normal operations, other for Copying to the destination bucket.
            //A. Create the bucket.
            bool hasCallbackArrived = false;

            S3ResponseEventHandler<object, ResponseEventArgs> handler = null;
            handler = delegate(object sender, ResponseEventArgs args)
            {
                IS3Response result = args.Response;
                //Unhook from event.
                _client.OnS3Response -= handler;
                hasCallbackArrived = true;
            };
            _client.OnS3Response += handler;

            PutBucketRequest request = new PutBucketRequest() { BucketName = _bucketName };
            _client.PutBucket(request);

            EnqueueConditional(() => hasCallbackArrived);

            //B. Create the destination bucket as well.
            bool hasDestinationCallbackArrived = false;
            S3ResponseEventHandler<object, ResponseEventArgs> destinationHandler = null;
            destinationHandler = delegate(object sender, ResponseEventArgs args)
            {
                IS3Response result = args.Response;
                //Unhook from event.
                _client.OnS3Response -= destinationHandler;
                hasDestinationCallbackArrived = true;
            };
            _client.OnS3Response += destinationHandler;

            PutBucketRequest requestDestination = new PutBucketRequest() { BucketName = _bucketNameDestination };
            _client.PutBucket(requestDestination);

            EnqueueConditional(() => hasDestinationCallbackArrived);

            EnqueueTestComplete();
        }