/// <summary> /// Dispose the underlying Amazon S3 instance in case this instance has ownership. /// </summary> public void Dispose() { if (_hasOwnership && _amazonS3 != null) { _amazonS3.Dispose(); } _amazonS3 = null; }
public void Dispose() { if (client != null) { client.Dispose(); } if (initResponse != null) { initResponse.Dispose(); } }
public bool UploadFileToS3(string uploadAsFileName, Stream ImageStream, S3CannedACL filePermission, S3StorageClass storageType) { try { AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client("AKIAJ4A6DAATIDU6ELAA", "EiA6EILkCp7pqzvnIUhXg3FFOft0j+pA/DtBM8if"); PutObjectRequest request = new PutObjectRequest(); request.WithKey("folder" + "/" + uploadAsFileName); request.WithInputStream(ImageStream); request.WithBucketName("shriners_rms"); request.CannedACL = filePermission; request.StorageClass = storageType; client.PutObject(request); client.Dispose(); } catch { return(false); } return(true); }
public async Task <Unit> Handle(CreateFoodCommand request, CancellationToken cancellationToken) { var entity = new Food { Name = request.Name, Price = request.Price, Description = request.Description, Weight = request.Weight, Size = request.Size, Protein = request.Protein, Fats = request.Fats, Carbohydrates = request.Carbohydrates, Calories = request.Calories, TypeId = request.TypeId }; if (request.Image != null) { AmazonS3 amazon = new AmazonS3(); string filename = amazon.GetRandomFileNameWithPrefix(request.Image.FileName, 100000, 5); string path = $"images/food/{filename}"; await amazon.UploadFoodImage(request.Image, filename); entity.ImagePath = path; amazon.Dispose(); } else { entity.ImagePath = "images/food/default.jpg"; } await _context.Food.AddAsync(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public static void ClassCleanup() { client.Dispose(); }
public static void AmazonUpload(string virtuelpath) { if (context.Cache[virtuelpath + _bucketServerName + "-file"] == null) { string filePath = context.Request.MapPath(virtuelpath); string contentType = "image/" + Path.GetExtension(filePath).Replace(".", null); // Create a signature for this operation PutObjectRequest putObjReq = new PutObjectRequest(); putObjReq.WithBucketName(_bucketServerName); putObjReq.WithContentType(contentType); putObjReq.WithFilePath(filePath); putObjReq.WithKey(Path.GetFileName(filePath)); putObjReq.WithCannedACL(S3CannedACL.PublicRead); var headers = new System.Collections.Specialized.NameValueCollection(); headers.Add("Expires", TimeZoneManager.DateTimeNow.AddMonths(6).ToString("ddd, dd MMM yyyy HH:mm:ss K")); putObjReq.AddHeaders(headers); //// COMPRESS file //MemoryStream ms = new MemoryStream(); //using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) //{ // byte[] buffer = File.ReadAllBytes(filePath); // zip.Write(buffer, 0, buffer.Length); // zip.Flush(); //} //ms.Position = 0; //// Create a signature for this operation //PutObjectRequest putObjReqGZ = new PutObjectRequest(); //putObjReqGZ.WithBucketName(_bucketServerName); //putObjReqGZ.WithContentType(contentType); //putObjReqGZ.WithInputStream(ms); //putObjReqGZ.WithKey(Path.GetFileName(filePath) + ".gz"); //putObjReqGZ.WithCannedACL(S3CannedACL.PublicRead); //var headersGZ = new System.Collections.Specialized.NameValueCollection(); //headersGZ.Add("Content-Encoding", "gzip"); //headersGZ.Add("Expires", TimeZoneManager.DateTimeNow.AddMonths(6).ToString("ddd, dd MMM yyyy HH:mm:ss K")); //putObjReqGZ.AddHeaders(headersGZ); // connect client AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(_amazonID, _amazonSecretKey); s3Client.PutObject(putObjReq); // upload file //s3Client.PutObject(putObjReqGZ); // upload file context.Cache.Add(virtuelpath.GetHashCode() + _bucketServerName + "-file", "isUploaded", null, TimeZoneManager.DateTimeNow.AddDays(30), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); // clean amazon s3Client.Dispose(); //ms.Close(); //ms.Flush(); //ms.Dispose(); } }
public static void AmazonUploadContent(string virtuelpath) { if (context.Cache[virtuelpath + _bucketServerName + "-content"] == null) { Uri path = new Uri(GetSiteRoot() + "/" + virtuelpath); string q = path.Query; string qp = HttpUtility.ParseQueryString(q).Get("p"); string qpageId = "-" + qp; string source = null; // Create a request using a URL WebClient wrGETURL = new System.Net.WebClient(); wrGETURL.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); wrGETURL.UseDefaultCredentials = false; wrGETURL.Encoding = Encoding.UTF8; source = wrGETURL.DownloadString(path); wrGETURL.Dispose(); wrGETURL.CancelAsync(); string contentType = "text/javascript"; // Create a signature for this operation PutObjectRequest putObjReq = new PutObjectRequest(); putObjReq.WithBucketName(_bucketServerName); putObjReq.WithContentType(contentType); putObjReq.WithContentBody(source); putObjReq.WithKey(virtuelpath.Remove(0).GetHashCode() + "-" + qp + ".js"); putObjReq.WithCannedACL(S3CannedACL.PublicRead); var headers = new System.Collections.Specialized.NameValueCollection(); headers.Add("Expires", TimeZoneManager.DateTimeNow.AddMonths(6).ToString("ddd, dd MMM yyyy HH:mm:ss K")); putObjReq.AddHeaders(headers); // COMPRESS file MemoryStream ms = new MemoryStream(); using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) { byte[] buffer = Encoding.UTF8.GetBytes(source); zip.Write(buffer, 0, buffer.Length); zip.Flush(); } ms.Position = 0; // Create a signature for this operation PutObjectRequest putObjReqGZ = new PutObjectRequest(); putObjReqGZ.WithBucketName(_bucketServerName); putObjReqGZ.WithContentType(contentType); putObjReqGZ.WithInputStream(ms); putObjReqGZ.WithKey(virtuelpath.Remove(0).GetHashCode() + "-" + qp + ".js.gz"); putObjReqGZ.WithCannedACL(S3CannedACL.PublicRead); var headersGZ = new System.Collections.Specialized.NameValueCollection(); headersGZ.Add("Content-Encoding", "gzip"); headersGZ.Add("Expires", TimeZoneManager.DateTimeNow.AddMonths(6).ToString("ddd, dd MMM yyyy HH:mm:ss K")); putObjReqGZ.AddHeaders(headersGZ); // connect client AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(_amazonID, _amazonSecretKey); s3Client.PutObject(putObjReq); // upload file s3Client.PutObject(putObjReqGZ); // upload file context.Cache.Add(virtuelpath + _bucketServerName + "-content", "isUploaded", null, TimeZoneManager.DateTimeNow.AddDays(30), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); // clean amazon s3Client.Dispose(); ms.Close(); ms.Flush(); ms.Dispose(); } }
public void Dispose() { s3client.Dispose(); }