/// <summary> /// Creates a new instance of the <see cref="S3UploadStreamProvider" /> class with the specified configuration settings. /// </summary> /// <param name="settings">The <see cref="UploadStreamProviderElement" /> object that holds the configuration settings.</param> public S3UploadStreamProvider(UploadStreamProviderElement settings) : base(settings) { string accessKeyId = Settings.Parameters["accessKeyId"]; string secretAccessKey = Settings.Parameters["secretAccessKey"]; string bucketName = Settings.Parameters["bucketName"]; bool useHttps; if (string.IsNullOrEmpty(accessKeyId)) throw new Exception("accessKeyId must be specified for SlickUpload S3 provider"); if (string.IsNullOrEmpty(secretAccessKey)) throw new Exception("secretAccessKey must be specified for SlickUpload S3 provider"); if (string.IsNullOrEmpty(bucketName)) throw new Exception("bucketName must be specified for SlickUpload S3 provider"); if (!bool.TryParse(Settings.Parameters["useHttps"], out useHttps)) useHttps = true; _client = new S3BlobClient(accessKeyId, secretAccessKey, bucketName, useHttps); _objectNameMethod = TypeFactory.ParseEnum<ObjectNameMethod>(Settings.Parameters["objectNameMethod"], ObjectNameMethod.Client); _cacheControl = Settings.Parameters["cacheControl"]; _contentDisposition = Settings.Parameters["contentDisposition"]; _contentEncoding = Settings.Parameters["contentEncoding"]; _cannedAcl = TypeFactory.ParseEnum<S3CannedAcl>(Settings.Parameters["cannedAcl"], S3CannedAcl.Private); _storageClass = TypeFactory.ParseEnum<S3StorageClass>(Settings.Parameters["storageClass"], S3StorageClass.Standard); }
public S3PostUploadRequest() { this.Metadata = new Dictionary<string, string>(); this.CannedACL = S3CannedACL.Private; this._actionStatus = HttpStatusCode.NoContent; this._storageClass = S3StorageClass.Standard; }
public S3Blobs(IAmazonS3 amazon, IPathManager pathManager, S3StorageClass s3StorageClass) : base(amazon) { _pathManager = pathManager ?? throw new ArgumentNullException(nameof(pathManager)); _s3StorageClass = s3StorageClass ?? throw new ArgumentNullException(nameof(s3StorageClass)); }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.StorageClass = sClass; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) setACLRequest.VersionId = copyResponse.VersionId; s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the storage class for the S3 Object Version to the value /// specified. /// </summary> /// <param name="s3ObjectVer">The S3 Object Version whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(S3ObjectVersion s3ObjectVer, S3StorageClass sClass, AmazonS3 s3Client) { SetObjectStorageClass(s3ObjectVer.BucketName, s3ObjectVer.Key, s3ObjectVer.VersionId, sClass, s3Client); }
/// <summary> /// Sets the StorageClass property for this request. /// Default: S3StorageClass.Standard. Set this property /// only if you want reduced redundancy for this object. /// Please refer to /// <see cref="T:Amazon.S3.Model.S3StorageClass"/> for /// information on S3 Storage Classes. /// </summary> /// <param name="sClass">The Storage Class to be set on the object</param> /// <returns>The request with the StorageClass set</returns> public InitiateMultipartUploadRequest WithStorageClass(S3StorageClass sClass) { this.storageClass = sClass; return this; }
/// <summary> /// Sets the StorageClass property for this request. /// Default: S3StorageClass.Standard. Set this property /// only if you want reduced redundancy for this object. /// Please refer to /// <see cref="T:Amazon.S3.Model.S3StorageClass"/> for /// information on S3 Storage Classes. /// </summary> /// <param name="sClass">The Storage Class to be set on the object</param> /// <returns>The request with the StorageClass set</returns> public InitiateMultipartUploadRequest WithStorageClass(S3StorageClass sClass) { this.storageClass = sClass; return(this); }
public AmazonS3WriterEtlOperation WithStorageClass(S3StorageClass storageClass) { _storageClass = storageClass; return(this); }
//private CancellationTokenSource CancellationTokenSource = new CancellationTokenSource(); private bool CreateBucket(string bucketName, Amazon.RegionEndpoint bucketRegion, S3StorageClass storageClass, out string statusMessage) { AWSCredentials awsCredentials = new BasicAWSCredentials(_account.AccessKey, _account.SecretKey); //TransferAgentOptions options = new TransferAgentOptions(); //ITransferAgent transferAgent = new S3TransferAgent(options, awsCredentials, _account.BucketName, CancellationTokenSource.Token); using (var client = new AmazonS3Client(awsCredentials, bucketRegion)) { bool alreadyExists = S3_DoesBucketExist(client, bucketName); if (alreadyExists) { statusMessage = "The informed bucket already exists. Please, pick another name."; return(false); } try { PutBucketRequest putRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true, }; PutBucketResponse response = client.PutBucket(putRequest); switch (response.HttpStatusCode) { default: statusMessage = "Unknown problem."; break; case HttpStatusCode.OK: statusMessage = "The bucket has been successfuly created."; return(true); case HttpStatusCode.Forbidden: statusMessage = string.Format("Bucket creation failed: {0}", response.ToString()); break; } } catch (Exception exception) { if (exception is AmazonS3Exception) { AmazonS3Exception amznException = exception as AmazonS3Exception; if (amznException.ErrorCode != null && (amznException.ErrorCode.Equals("InvalidAccessKeyId") || amznException.ErrorCode.Equals("InvalidSecurity"))) { statusMessage = "Check the provided AWS Credentials."; } else { statusMessage = string.Format("Error occurred. Message:'{0}' when writing an object", amznException.Message); } } else { statusMessage = string.Format("Exception occurred: {0}", exception.Message); } } } return(false); }
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 void Execute() { Console.WriteLine("doc Started."); try { //fetch all the resources to be uploaded AssetDataFactory _dataFactory = new AssetDataFactory(); AmazonS3UploadHelper cdnManager = new AmazonS3UploadHelper(); int chunckSize = 0; int defaultchunckSize = 30000; Console.WriteLine("Fetch asset for syncing"); int totalCount = _dataFactory.GetAppointmentAssetCount((int)AssetTypes.Appointment_Document, true); int chunkCounter = 1; int chunkTotal = totalCount / defaultchunckSize; if (chunkTotal == 0) { chunkTotal++; } Console.WriteLine("Upload asset will take place in chunks"); Console.WriteLine("Upload asset started at {0}", DateTime.Now.ToLongTimeString()); DateTime dtStartDateTime = DateTime.Now; do { List <Assets> assets = _dataFactory.GetAppointmentAsset((int)AssetTypes.Appointment_Document, true, defaultchunckSize);//asset type //loop through each asset and upload to CDN int counter = 1; foreach (var item in assets) { ProgressHelper.ShowPercentProgress(string.Format("Uploading documents of chunk {0} out of {1} of which ", chunkCounter, chunkTotal), counter, assets.Count); counter++; try { var assetRequest = new UploadAssetRequestParameters(); item.TargetPath = item.TargetPath.ToLower(); item.TargetPath = item.TargetPath.Replace(@"\\", @"/").Replace(@"\", @"/"); //string directoryName = Path.GetDirectoryName(item.TargetPath); int pos = item.TargetPath.LastIndexOf("/"); string bucketRelativePath = item.TargetPath.Substring(0, pos); assetRequest.BucketName = ConfigurationManager.AppSettings["S3BucketName"] + @"/private/" + bucketRelativePath; assetRequest.FilePath = ConfigurationManager.AppSettings["FileStoreRoot"] + item.TargetPath; assetRequest.Tags = new Dictionary <string, string>() { { "Asset Type", "Appointment Document" }, { "Asset Id", item.AssetId.ToString() } }; if (item.AppointmentAssets.Count > 0) { assetRequest.Tags.Add("Asset Entity Id", item.AppointmentAssets.FirstOrDefault().appointmentID.ToString()); } int monthDiff = S3Uploader.GetMonthDifference(DateTime.UtcNow, item.CreatedDateTime ?? DateTime.UtcNow); S3StorageClass storageClass = S3StorageClass.Standard; if (monthDiff > 6) { storageClass = S3StorageClass.StandardInfrequentAccess; } //else if (monthDiff > 36) // storageClass = S3StorageClass.Glacier; assetRequest.AssetId = item.AssetId; assetRequest.s3StorageClass = storageClass; //Uploading file string extension = Path.GetExtension(item.TargetPath); if (!string.IsNullOrEmpty(extension) && extension == "pdf") { assetRequest.ContentType = "application/pdf"; } if (storageClass != S3StorageClass.Glacier) { string objectKey = cdnManager.UploadFile(assetRequest); if (!string.IsNullOrEmpty(objectKey)) { _dataFactory.UpdateAssetPath(item.AssetId, bucketRelativePath + @"/" + objectKey, @"private/" + bucketRelativePath + @"/" + objectKey); } } } catch (Exception ex) { ErrorUtility.LogError(ex, GartnerApplication.Unknown, string.Format("Error: upload image Asset (Asset Id : {0}) error: {1}", item.AssetId, ex.Message)); Console.WriteLine(ex.Message); } } chunkCounter++; chunckSize = chunckSize + defaultchunckSize; } while (totalCount > chunckSize); DateTime dtEndDateTime = DateTime.Now; Console.WriteLine("Uploading of appointment documents completed at {0}", dtEndDateTime.ToLongTimeString()); Console.WriteLine("Time took to upload appointment documents is {0}", (dtEndDateTime - dtStartDateTime).Duration().TotalMinutes); } catch (Exception e) { ErrorUtility.LogError(e, GartnerApplication.Unknown, string.Format("Error: Upload Doc error: " + e.Message)); Console.WriteLine("Exception while uploading document : {0}", e.Message); } }
/// <summary> /// /// </summary> /// <param name="filePath"></param> /// <param name="key"></param> /// <param name="bucketName"></param> /// <param name="storageClass"></param> /// <returns></returns> public async Task UploadFileAsync(string filePath, string key, string bucketName = null, S3StorageClass storageClass = null) { var uploadRequest = new TransferUtilityUploadRequest { FilePath = filePath, BucketName = bucketName ?? _bucketName, Key = key, CannedACL = S3CannedACL.PublicRead, StorageClass = storageClass ?? S3StorageClass.Standard }; await _transferUtility.UploadAsync(uploadRequest, CancellationToken.None); await _amazonS3.MakeObjectPublicAsync(bucketName ?? _bucketName, key, true); }
IAmazonS3WriterConfiguration IAmazonS3WriterConfiguration.WithStorageClass(S3StorageClass storageClass) => WithStorageClass(storageClass);
///// <summary> ///// Sets the storage class for the S3 Object Version to the value ///// specified. ///// </summary> ///// <param name="s3ObjectVer">The S3 Object Version whose storage class needs changing</param> ///// <param name="sClass">The new Storage Class for the object</param> ///// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> ///// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> //public static void SetObjectStorageClass(S3ObjectVersion s3ObjectVer, S3StorageClass sClass, IAmazonS3 s3Client) //{ // SetObjectStorageClass(s3ObjectVer.BucketName, s3ObjectVer.Key, s3ObjectVer.VersionId, sClass, s3Client); //} /// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(IAmazonS3 s3Client, string bucketName, string key, string version, S3StorageClass sClass) { CopyObjectRequest copyRequest; PutACLRequest putACLRequest; SetupForObjectModification(s3Client, bucketName, key, version, out copyRequest, out putACLRequest); copyRequest.StorageClass = sClass; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.SourceVersionId)) { putACLRequest.VersionId = copyResponse.SourceVersionId; } s3Client.PutACL(putACLRequest); }
/// <summary> /// Sets the storage class for the S3 Object to the value /// specified. /// </summary> /// <param name="s3Object">The S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(S3Object s3Object, S3StorageClass sClass, AmazonS3 s3Client) { SetObjectStorageClass(s3Object.BucketName, s3Object.Key, sClass, s3Client); }
/// <summary> /// Sets the StorageClass property for this request. /// </summary> /// <param name="storageClass">the value that StorageClass is set to.</param> /// <returns>this instance</returns> public LifecycleTransition WithStorageClass(S3StorageClass storageClass) { this.StorageClass = storageClass; return this; }
/// <summary> /// Sets the storage class for the uploaded Amazon S3 object. /// and returns this object instance, /// enabling additional method calls to be chained together. /// Please refer to /// <see cref="T:Amazon.S3.Model.S3StorageClass"/> for /// information on S3 Storage Classes. /// </summary> /// <param name="sClass"> /// The storage class for the uploaded Amazon S3 object. /// </param> /// <returns> /// This object instance, enabling additional method calls to be chained together. /// </returns> public TransferUtilityUploadDirectoryRequest WithStorageClass(S3StorageClass sClass) { this._storageClass = sClass; return this; }
void IXmlSerializable.ReadXml(System.Xml.XmlReader reader) { string value; while (reader.NodeType != System.Xml.XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == System.Xml.XmlNodeType.Element) { switch(reader.Name) { case "Date": value = reader.ReadElementContentAsString(); if (!string.IsNullOrEmpty(value)) this.Date = DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); break; case "Days": value = reader.ReadElementContentAsString(); if (!string.IsNullOrEmpty(value)) this.Days = int.Parse(value, CultureInfo.InvariantCulture); break; case "StorageClass": value = reader.ReadElementContentAsString(); if (!string.IsNullOrEmpty(value)) { this.StorageClass = Util.AmazonS3Util.ConvertToS3StorageClass(value); } break; } } } reader.Read(); }
/// <summary> /// Sets a StorageClass for the S3 object. /// Default: S3StorageClass.Standard. /// </summary> /// <remarks> /// Set this property only if you want reduced redundancy for this object. /// Please refer to <see cref="T:Amazon.S3.Model.S3StorageClass"/> for /// information on S3 Storage Classes. /// </remarks> /// <param name="sClass">The Storage Class to be set on the object</param> /// <returns>The request with the StorageClass set</returns> public PutObjectRequest WithStorageClass(S3StorageClass sClass) { this.storageClass = sClass; return(this); }
#pragma warning disable S3242 // JObject is more descriptive than 'more general' IDictionary private static (string ControlValue, byte[] ToEncrypt, string Protector, string ApplicationName, S3XmlRepositoryConfig S3Config, KmsXmlEncryptorConfig KmsConfig) ToOldConfig(JObject config) #pragma warning restore S3242 { var controlValue = config[ConfigType].Value <string>(); var protectData = Convert.FromBase64String(config[DataToProtect].Value <string>()); var protectorValue = config[ProtectorKey].Value <string>(); var old = new S3XmlRepositoryConfig(config[nameof(S3XmlRepositoryConfig.Bucket)].Value <string>()); if (config.TryGetValue(nameof(S3XmlRepositoryConfig.KeyPrefix), out JToken keyprefix)) { old.KeyPrefix = keyprefix.Value <string>(); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.MaxS3QueryConcurrency), out JToken concurrency)) { old.MaxS3QueryConcurrency = concurrency.Value <int>(); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.StorageClass), out JToken storageClass)) { old.StorageClass = S3StorageClass.FindValue(storageClass.Value <string>()); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.ServerSideEncryptionMethod), out JToken serverSideEncryptionMethod)) { old.ServerSideEncryptionMethod = ServerSideEncryptionMethod.FindValue(serverSideEncryptionMethod.Value <string>()); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.ServerSideEncryptionCustomerMethod), out JToken serverSideEncryptionCustomerMethod)) { old.ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.FindValue(serverSideEncryptionCustomerMethod.Value <string>()); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.ServerSideEncryptionCustomerProvidedKey), out JToken serverSideEncryptionCustomerProvidedKey)) { old.ServerSideEncryptionCustomerProvidedKey = serverSideEncryptionCustomerProvidedKey.Value <string>(); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.ServerSideEncryptionCustomerProvidedKeyMd5), out JToken serverSideEncryptionCustomerProvidedKeyMd5)) { old.ServerSideEncryptionCustomerProvidedKeyMd5 = serverSideEncryptionCustomerProvidedKeyMd5.Value <string>(); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.ServerSideEncryptionKeyManagementServiceKeyId), out JToken serverSideEncryptionKeyManagementServiceKeyId)) { old.ServerSideEncryptionKeyManagementServiceKeyId = serverSideEncryptionKeyManagementServiceKeyId.Value <string>(); } if (config.TryGetValue(nameof(S3XmlRepositoryConfig.ClientSideCompression), out JToken clientSideCompression)) { old.ClientSideCompression = clientSideCompression.Value <bool>(); } string applicationName = null; if (config.TryGetValue(ApplicationNameKey, out JToken appName)) { applicationName = appName.Value <string>(); } string kmsApplicationName = null; if (config.TryGetValue(KmsApplicationNameKey, out JToken kmsAppName)) { kmsApplicationName = kmsAppName.Value <string>(); } string keyIdentifier = null; if (config.TryGetValue(nameof(KmsXmlEncryptorConfig.KeyId), out JToken keyId)) { keyIdentifier = keyId.Value <string>(); } KmsXmlEncryptorConfig kmsConfig = null; if (!string.IsNullOrEmpty(kmsApplicationName) && !string.IsNullOrEmpty(keyIdentifier)) { kmsConfig = new KmsXmlEncryptorConfig(kmsApplicationName, keyIdentifier); } return(controlValue, protectData, protectorValue, applicationName, old, kmsConfig); }
/// <summary> /// Uploads a file to S3. This method uses the "TransferUtility" class in order to upload the file. /// </summary> public bool FileUpload(string bucketname, string dataname, string filepath, S3StorageClass storageClass) { return(FileUpload(bucketname, dataname, filepath, storageClass, S3CannedACL.PublicReadWrite)); }
public void Execute() { Console.WriteLine("audio asset Syn Started."); try { //fetch all the resources to be uploaded AssetDataFactory _dataFactory = new AssetDataFactory(); Console.WriteLine("Fetch asset for syncing"); List <Assets> assets = _dataFactory.GetAppointmentAsset((int)AssetTypes.Appointment_Audio, true);//asset type Console.WriteLine("asset has been fetched and now upload to S3 will be done"); AmazonS3UploadHelper cdnManager = new AmazonS3UploadHelper(); //loop through each asset and upload to CDN Console.WriteLine("Upload asset started at {0}", DateTime.Now.ToLongTimeString()); DateTime dtStartDateTime = DateTime.Now; int counter = 1; foreach (var item in assets) { ProgressHelper.ShowPercentProgress("progress of audio upload ", counter, assets.Count()); counter++; try { var assetRequest = new UploadAssetRequestParameters(); int pos = item.TargetPath.LastIndexOf("/"); string bucketRelativePath = item.TargetPath.Substring(0, pos); assetRequest.BucketName = ConfigurationManager.AppSettings["S3BucketName"] + @"/private/" + bucketRelativePath; assetRequest.BucketName = assetRequest.BucketName.ToLower(); assetRequest.BucketName = assetRequest.BucketName.Replace(@"\\", @"/").Replace(@"\", @"/"); assetRequest.FilePath = ConfigurationManager.AppSettings["FileStoreRoot"] + item.TargetPath; assetRequest.Tags = new Dictionary <string, string>() { { "Asset Type", "Appointment Audio" }, { "Asset Id", item.AssetId.ToString() } }; if (item.AppointmentAssets.Count > 0) { assetRequest.Tags.Add("Asset Entity Id", item.AppointmentAssets.FirstOrDefault().appointmentID.ToString()); } int monthDiff = S3Uploader.GetMonthDifference(DateTime.UtcNow, item.UpdatedDateTime ?? item.CreatedDateTime.Value); S3StorageClass storageClass = S3StorageClass.Standard; if (monthDiff > 6) { storageClass = S3StorageClass.StandardInfrequentAccess; } assetRequest.AssetId = item.AssetId; assetRequest.s3StorageClass = storageClass; //Uploading file string objectKey = cdnManager.UploadFile(assetRequest); if (!string.IsNullOrEmpty(objectKey)) { _dataFactory.UpdateAssetPath(item.AssetId, bucketRelativePath + @"/" + objectKey, @"private/" + bucketRelativePath + @"/" + objectKey); } } catch (Exception ex) { ErrorUtility.LogError(ex, GartnerApplication.Unknown, string.Format("Error: upload image Asset (Asset Id : {0}) error: {1}", item.AssetId, ex.Message)); Console.WriteLine(ex.Message); } } DateTime dtEndDateTime = DateTime.Now; Console.WriteLine("Uploading of audio files completed at {0}", dtEndDateTime.ToLongTimeString()); Console.WriteLine("Time took to upload audio files is {0}", (dtEndDateTime - dtStartDateTime).Duration().TotalMinutes); } catch (Exception ex) { ErrorUtility.LogError(ex, GartnerApplication.Unknown, string.Format("Error: upload image error: " + ex.Message)); Console.WriteLine(ex.Message); } }
public S3ObjectResponse(string bucketName, string eTag, string key, DateTime lastModified, long size, S3StorageClass storageClass) { this.bucketName = bucketName; this.eTag = eTag; this.key = key; this.lastModified = lastModified; this.size = size; this.storageClass = storageClass; }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { if (sClass > S3StorageClass.ReducedRedundancy || sClass < S3StorageClass.Standard) { throw new ArgumentException("Invalid value specified for storage class."); } if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // Get the existing ACL of the object GetACLRequest getACLRequest = new GetACLRequest(); getACLRequest.BucketName = bucketName; getACLRequest.Key = key; if (version != null) { getACLRequest.VersionId = version; } GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest); GetObjectMetadataResponse getMetadataResponse = s3Client.GetObjectMetadata(new GetObjectMetadataRequest() .WithBucketName(bucketName) .WithKey(key)); // Set the storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; copyRequest.ServerSideEncryptionMethod = getMetadataResponse.ServerSideEncryptionMethod; if (version != null) { copyRequest.SourceVersionId = version; } copyRequest.StorageClass = sClass; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) { setACLRequest.VersionId = copyResponse.VersionId; } setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }
public string UploadFile(UploadAssetRequestParameters request, S3StorageClass storageClass, long assetId) { string objectKey = GetS3FileName(request.FilePath, assetId); try { List <Amazon.S3.Model.Tag> tagSet = new List <Amazon.S3.Model.Tag>(); if (request.Tags != null) { foreach (KeyValuePair <string, string> entry in request.Tags) { tagSet.Add(new Amazon.S3.Model.Tag { Key = entry.Key, Value = entry.Value }); } } var awsRegion = Amazon.RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings["AWSRegion"].ToString()); IAmazonS3 client = new AmazonS3Client(awsRegion); PutObjectRequest putRequest = new PutObjectRequest { BucketName = request.BucketName, Key = objectKey, TagSet = tagSet, StorageClass = storageClass }; //upload by filepath or memorystream if (request.InputStream != null && request.InputStream.Length > 0) { putRequest.InputStream = new MemoryStream(request.InputStream); } else { putRequest.FilePath = request.FilePath; } PutObjectResponse response = client.PutObject(putRequest); return(objectKey);//to fetch the uploaded content append cloudfrontUrl + objectKey } catch (AmazonS3Exception amazonS3Exception) { string errorMessage = String.Empty; if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Check the provided AWS Credentials."); } else { errorMessage = string.Format("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message); ErrorUtility.LogError(amazonS3Exception, GartnerApplication.UtilityService, string.Format("Error: AWS: UploadFile: " + errorMessage)); } } catch (Exception e) { ErrorUtility.LogError(e, GartnerApplication.Unknown, string.Format("Error: UploadFile: " + e.Message)); } return(string.Empty);//empty string denote that process has got some error }
/// <summary> /// S3 storage class for the copy of the source object. By default the new object uses the same storage class as the source object. /// </summary> /// <remarks> /// Set this property only if you want to change the storage /// class of the destination object. Please refer /// <see cref="T:Amazon.S3.Model.S3StorageClass"/> for /// information on S3 Storage Classes. /// </remarks> /// <param name="sClass">The storage class to be set on the new object</param> /// <returns>The request with the StorageClass set</returns> public CopyObjectRequest WithStorageClass(S3StorageClass sClass) { this.StorageClass = sClass; return this; }
/// <summary> /// Sets the StorageClass property for this request. /// </summary> /// <param name="storageClass">the value that StorageClass is set to.</param> /// <returns>this instance</returns> public LifecycleTransition WithStorageClass(S3StorageClass storageClass) { this.StorageClass = storageClass; return(this); }
///// <summary> ///// Sets the storage class for the S3 Object to the value ///// specified. ///// </summary> ///// <param name="s3Object">The S3 Object whose storage class needs changing</param> ///// <param name="sClass">The new Storage Class for the object</param> ///// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> ///// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> //public static void SetObjectStorageClass(S3Object s3Object, S3StorageClass sClass, IAmazonS3 s3Client) //{ // SetObjectStorageClass(s3Object.BucketName, s3Object.Key, sClass, s3Client); //} /// <summary> /// Sets the storage class for the S3 Object to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(IAmazonS3 s3Client, string bucketName, string key, S3StorageClass sClass) { SetObjectStorageClass(s3Client, bucketName, key, null, sClass); }
public CopyObjectRequest WithStorageClass(S3StorageClass sClass) { this.StorageClass = sClass; return(this); }
/// <summary> /// Sets the storage class for the uploaded Amazon S3 object. /// and returns this object instance, /// enabling additional method calls to be chained together. /// Please refer to /// <see cref="T:Amazon.S3.Model.S3StorageClass"/> for /// information on S3 Storage Classes. /// </summary> /// <param name="sClass"> /// The storage class for the uploaded Amazon S3 object. /// </param> /// <returns> /// This object instance, enabling additional method calls to be chained together. /// </returns> public TransferUtilityUploadDirectoryRequest WithStorageClass(S3StorageClass sClass) { this._storageClass = sClass; return(this); }
///// <summary> ///// Sets the storage class for the S3 Object Version to the value ///// specified. ///// </summary> ///// <param name="s3ObjectVer">The S3 Object Version whose storage class needs changing</param> ///// <param name="sClass">The new Storage Class for the object</param> ///// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> ///// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> //public static void SetObjectStorageClass(S3ObjectVersion s3ObjectVer, S3StorageClass sClass, IAmazonS3 s3Client) //{ // SetObjectStorageClass(s3ObjectVer.BucketName, s3ObjectVer.Key, s3ObjectVer.VersionId, sClass, s3Client); //} /// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(IAmazonS3 s3Client, string bucketName, string key, string version, S3StorageClass sClass) { CopyObjectRequest copyRequest; PutACLRequest putACLRequest; SetupForObjectModification(s3Client, bucketName, key, version, out copyRequest, out putACLRequest); copyRequest.StorageClass = sClass; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.SourceVersionId)) putACLRequest.VersionId = copyResponse.SourceVersionId; s3Client.PutACL(putACLRequest); }
/// <summary> /// Sets the storage class for the S3 Object to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, S3StorageClass sClass, AmazonS3 s3Client) { SetObjectStorageClass(bucketName, key, null, sClass, s3Client); }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.StorageClass = sClass; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) { setACLRequest.VersionId = copyResponse.VersionId; } s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { if (sClass > S3StorageClass.ReducedRedundancy || sClass < S3StorageClass.Standard) { throw new ArgumentException("Invalid value specified for storage class."); } if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // Get the existing ACL of the object GetACLRequest getACLRequest = new GetACLRequest(); getACLRequest.BucketName = bucketName; getACLRequest.Key = key; if(version != null) getACLRequest.VersionId = version; GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest); // Set the storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; if (version != null) copyRequest.SourceVersionId = version; copyRequest.StorageClass = sClass; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) setACLRequest.VersionId = copyResponse.VersionId; setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }
public async Task <ImageUploadedModel> UploadImage( string bucketName, string bucketUrl, string objectKey, S3StorageClass storageClass, S3CannedACL permissions, string glacierVaultName, ImageInfo image) { ImageUploadedModel model = new ImageUploadedModel(); try { PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName, Key = objectKey, StorageClass = storageClass, CannedACL = permissions, ContentType = image.MimeType, AutoCloseStream = false }; putRequest.Metadata.Add("width", image.Width.ToString()); putRequest.Metadata.Add("height", image.Height.ToString()); putRequest.InputStream = image.Image; byte[] md5Hash = image.Image.Md5Hash(); putRequest.MD5Digest = md5Hash.ToBase64String(); PutObjectResponse response = await S3Client.PutObjectAsync(putRequest); string eTag = response.ETag.Trim('"').ToLowerInvariant(); string expectedETag = md5Hash.ToS3ETagString(); if (eTag != expectedETag) { throw new Exception("The eTag received from S3 doesn't match the eTag computed before uploading. This usually indicates that the image has been corrupted in transit."); } // upload to Glacier if needed if (!string.IsNullOrWhiteSpace(glacierVaultName)) { ArchiveDescription description = new ArchiveDescription { ObjectKey = objectKey, ContentType = image.MimeType, Width = image.Width, Height = image.Height }; // reset stream position in image image.Image.Position = 0; UploadArchiveRequest glacierRequest = new UploadArchiveRequest { ArchiveDescription = JsonConvert.SerializeObject(description, Formatting.None), Body = image.Image, VaultName = glacierVaultName, Checksum = TreeHashGenerator.CalculateTreeHash(image.Image) }; UploadArchiveResponse glacierResponse = await GlacierClient.UploadArchiveAsync(glacierRequest); model.ArchiveId = glacierResponse.ArchiveId; } model.ObjectKey = objectKey; model.ETag = eTag; model.ObjectLocation = bucketUrl + objectKey; model.VersionId = response.VersionId; } catch (Exception ex) { model.Exception = ex; } return(model); }
public PutObjectRequest WithStorageClass(S3StorageClass sClass) { this.storageClass = sClass; return this; }
public S3WriteContext(IS3Mediator s3, string bucketName, S3StorageClass storageClass) : base(s3, bucketName) { StorageClass = storageClass ?? throw new System.ArgumentNullException(nameof(storageClass)); }