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; }*/ }
public static void bucketSerial(String filePath) { System.Console.WriteLine("\nhello,Bucket!!"); NameValueCollection appConfig = ConfigurationManager.AppSettings; AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(appConfig["AWSAccessKey"], appConfig["AWSSecretKey"]); String bucketName = "chttest2"; //String bucketRegionName = "EU"; //PutBucket PutBucketRequest Brequest = new PutBucketRequest(); Brequest.WithBucketName(bucketName); //request.WithBucketRegionName(bucketRegionName); //Brequest.WithBucketRegion(S3Region.EU); Brequest.WithCannedACL(S3CannedACL.PublicRead); PutBucketResponse response = s3Client.PutBucket(Brequest); //PutBucketResponse response = s3Client.PutBucket(new PutBucketRequest().WithBucketName(bucketName)); //GetService ListBucketsResponse result = s3Client.ListBuckets(); System.Console.WriteLine("Get Service Result:\n {0}\n", result.ResponseXml); //******************************************************************************************************************************** //PutObject-apple.jpg System.Console.WriteLine("PutObject!\n"); s3Client.PutObject(new PutObjectRequest().WithBucketName(bucketName).WithKey("apple.jpg").WithFilePath(filePath)); //PutObject-sample.jpg s3Client.PutObject(new PutObjectRequest().WithBucketName(bucketName).WithKey("photos/2006/January/sample.jpg").WithFilePath(filePath)); //PutObject-sample2.jpg s3Client.PutObject(new PutObjectRequest().WithBucketName(bucketName).WithKey("photos/2006/January/sample2.jpg").WithFilePath(filePath)); //PutObject-asset.txt s3Client.PutObject(new PutObjectRequest().WithBucketName(bucketName).WithKey("asset.txt").WithFilePath(filePath)); //******************************************************************************************************************************** //GetBucket ListObjectsResponse objects = s3Client.ListObjects(new ListObjectsRequest().WithBucketName(bucketName)); System.Console.WriteLine("Get Bucket Result:\n {0}\n", objects.ResponseXml); ListObjectsResponse Prefixobjects = s3Client.ListObjects(new ListObjectsRequest().WithBucketName(bucketName).WithPrefix("photos/")); System.Console.WriteLine("Get Bucket With Prefix Result:\n {0}\n", Prefixobjects.ResponseXml); ListObjectsResponse Delimiterobjects = s3Client.ListObjects(new ListObjectsRequest().WithBucketName(bucketName).WithDelimiter("/")); System.Console.WriteLine("Get Bucket With Delimiter Result:\n {0}\n", Delimiterobjects.ResponseXml); ListObjectsResponse PDobjects = s3Client.ListObjects(new ListObjectsRequest().WithBucketName(bucketName).WithDelimiter("/").WithPrefix("photos/")); System.Console.WriteLine("Get Bucket With delimeter & prefix Result:\n {0}\n", PDobjects.ResponseXml); ListObjectsResponse MaxKeyobjects = s3Client.ListObjects(new ListObjectsRequest().WithBucketName(bucketName).WithMaxKeys(2)); System.Console.WriteLine("Get Bucket With MaxKey Result:\n {0}\n", MaxKeyobjects.ResponseXml); ListObjectsResponse Markerobjects = s3Client.ListObjects(new ListObjectsRequest().WithBucketName(bucketName).WithMarker("apple.jpg")); System.Console.WriteLine("Get Bucket With Marker Result:\n {0}\n", Markerobjects.ResponseXml); //******************************************************************************************************************************** //DeleteObject-apple.jpg System.Console.WriteLine("Delete Object!\n"); s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("apple.jpg")); //DeleteObject-sample.jpg s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("photos/2006/January/sample.jpg")); //DeleteObject-sample2.jpg s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("photos/2006/January/sample2.jpg")); //DeleteObject-asset.txt s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("asset.txt")); //******************************************************************************************************************************** //DeleteBucket System.Console.WriteLine("Delete Bucket!"); s3Client.DeleteBucket(new DeleteBucketRequest().WithBucketName(bucketName)); System.Console.WriteLine("END!"); }