/// <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 static void ShareFile(AmazonS3 s3Client, string filekey) { S3Response response1 = s3Client.SetACL(new SetACLRequest() { CannedACL = S3CannedACL.PublicRead, BucketName = BUCKET_NAME, Key = filekey }); }
/// <summary> /// Sets the server side encryption method 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</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</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 SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { 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); ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest() .WithBucketName(bucketName) .WithPrefix(key) .WithMaxKeys(1)); if (listObjectResponse.S3Objects.Count != 1) { throw new ArgumentNullException("No object exists with this bucket name and key."); } // Set the storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; copyRequest.StorageClass = listObjectResponse.S3Objects[0].StorageClass == "STANDARD" ? S3StorageClass.Standard : S3StorageClass.ReducedRedundancy; if (version != null) { copyRequest.SourceVersionId = version; } copyRequest.ServerSideEncryptionMethod = method; // 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); }
/// <summary> /// Set access rights of an object. /// </summary> /// <param name="bucketName">The name of the bucket.</param> /// <param name="key">The key of the object.</param> /// <param name="acl">The desired access rights.</param> public void SetAcl(string bucketName, string key, S3CannedACL acl) { var request = new SetACLRequest { BucketName = bucketName, CannedACL = acl, Key = key }; _amazonS3.SetACL(request); }
/// <summary> /// Sets the redirect location for the S3 Object's when being accessed through the S3 website endpoint. /// </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</param> /// <param name="websiteRedirectLocation">The redirect location</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetWebsiteRedirectLocation(string bucketName, string key, string websiteRedirectLocation, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, null, s3Client, out copyRequest, out setACLRequest); copyRequest.WebsiteRedirectLocation = websiteRedirectLocation; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) { setACLRequest.VersionId = copyResponse.VersionId; } s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the server side encryption method 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</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.ServerSideEncryptionMethod = method; 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) { 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); }
private void setS3Permission(String bucketName, String key) { // Get the ACL for the file and retrieve the owner ID (not sure how to get it otherwise). GetACLRequest getAclRequest = new GetACLRequest().WithBucketName(bucketName).WithKey(key); GetACLResponse aclResponse = s3.GetACL(getAclRequest); Owner owner = aclResponse.AccessControlList.Owner; // Create a grantee as the MessageGears account S3Grantee grantee = new S3Grantee().WithCanonicalUser(properties.MessageGearsAWSCanonicalId, "MessageGears"); // Grant MessageGears Read-only access S3Permission messageGearsPermission = S3Permission.READ; S3AccessControlList acl = new S3AccessControlList().WithOwner(owner); acl.AddGrant(grantee, messageGearsPermission); // Create a new ACL granting the owner full control. grantee = new S3Grantee().WithCanonicalUser(owner.Id, "MyAWSId"); acl.AddGrant(grantee, S3Permission.FULL_CONTROL); SetACLRequest aclRequest = new SetACLRequest().WithACL(acl).WithBucketName(bucketName).WithKey(key); s3.SetACL(aclRequest); }
/// <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); }
/// <summary> /// Sets the redirect location for the S3 Object's when being accessed through the S3 website endpoint. /// </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</param> /// <param name="websiteRedirectLocation">The redirect location</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetWebsiteRedirectLocation(string bucketName, string key, string websiteRedirectLocation, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, null, s3Client, out copyRequest, out setACLRequest); copyRequest.WebsiteRedirectLocation = websiteRedirectLocation; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) setACLRequest.VersionId = copyResponse.VersionId; s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the server side encryption method 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</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.ServerSideEncryptionMethod = method; 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) { 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 server side encryption method 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</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</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 SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { 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); ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest() .WithBucketName(bucketName) .WithPrefix(key) .WithMaxKeys(1)); if (listObjectResponse.S3Objects.Count != 1) { throw new ArgumentNullException("No object exists with this bucket name and key."); } // Set the storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; copyRequest.StorageClass = listObjectResponse.S3Objects[0].StorageClass == "STANDARD" ? S3StorageClass.Standard : S3StorageClass.ReducedRedundancy; if (version != null) copyRequest.SourceVersionId = version; copyRequest.ServerSideEncryptionMethod = method; // 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); }