/// <summary> /// This method first retrieves and then clears the ACL for an object. /// </summary> /// <param name="client">The initialized S3 client object which will be /// used to get and change the ACL for the S3 object.</param> /// <param name="bucketName">A string representing the name of the S3 /// bucket where the object whose ACL will be modified is stored.</param> /// <param name="keyName">The key name of the S3 object whose ACL will /// be modified.</param> /// <param name="emailAddress">The email address to use in defining the /// grant for the new ACL.</param> public static async Task TestObjectACLTestAsync(IAmazonS3 client, string bucketName, string keyName, string emailAddress) { try { // Retrieve the ACL for the object. GetACLResponse aclResponse = await client.GetACLAsync(new GetACLRequest { BucketName = bucketName, Key = keyName, }); S3AccessControlList acl = aclResponse.AccessControlList; // Retrieve the owner (we use this to re-add permissions after we clear the ACL). Owner owner = acl.Owner; // Clear existing grants. acl.Grants.Clear(); // Add a grant to reset the owner's full permission (the previous clear statement removed all permissions). S3Grant fullControlGrant = new () { Grantee = new S3Grantee { CanonicalUser = owner.Id }, Permission = S3Permission.FULL_CONTROL, }; // Describe the grant for the permission using an email address. S3Grant grantUsingEmail = new () { Grantee = new S3Grantee { EmailAddress = emailAddress }, Permission = S3Permission.WRITE_ACP, }; acl.Grants.AddRange(new List <S3Grant> { fullControlGrant, grantUsingEmail }); // Set a new ACL. PutACLResponse response = await client.PutACLAsync(new PutACLRequest { BucketName = bucketName, Key = keyName, AccessControlList = acl, }); } catch (AmazonS3Exception amazonS3Exception) { Console.WriteLine($"Error: {amazonS3Exception.Message}"); } }
static void WriteObjectAcl() { string id = Common.InputString("Key:", null, false); string owner = Common.InputString("Owner:", "default", false); try { PutACLRequest request = new PutACLRequest { BucketName = _Bucket, Key = id }; request.Key = id; request.AccessControlList = new S3AccessControlList(); request.AccessControlList.Owner = new Owner(); request.AccessControlList.Owner.DisplayName = owner; request.AccessControlList.Grants = new List <S3Grant>(); S3Grant grant = new S3Grant(); S3Grantee grantee = new S3Grantee(); grantee.CanonicalUser = owner; grantee.DisplayName = owner; grantee.EmailAddress = owner; grant.Grantee = grantee; request.AccessControlList.Grants.Add(grant); PutACLResponse response = _S3Client.PutACLAsync(request).Result; int statusCode = (int)response.HttpStatusCode; if (response != null) { Console.WriteLine("Success"); return; } else { Console.WriteLine("Failed"); return; } } catch (Exception e) { Console.WriteLine(Common.SerializeJson(e, true)); } }
static async Task WritingAnObjectAsync() { try { S3Grant s = new S3Grant(); s.Permission = S3Permission.READ; S3Grantee grantee = new S3Grantee(); grantee.URI = "http://acs.amazonaws.com/groups/global/AllUsers";//(https://www.cloudconformity.com/knowledge-base/aws/S3/s3-bucket-public-read-access.html) s.Grantee = grantee; List <S3Grant> list = new List <S3Grant>(); list.Add(s); var putRequest2 = new PutObjectRequest { BucketName = bucketName, Key = keyName, FilePath = filePath, ContentType = "image/png", Grants = list, }; putRequest2.Metadata.Add("x-amz-meta-title", "someTitle"); PutObjectResponse response2 = await client.PutObjectAsync(putRequest2); } catch (AmazonS3Exception e) { Console.WriteLine( "Error encountered ***. Message:'{0}' when writing an object" , e.Message); } catch (System.Exception e) { Console.WriteLine( "Unknown encountered on server. Message: '{0}' when writing an object", e.Message ); } }
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); }
public void SetACL(string fileKey, bool anonymouseReadAccess) { SetACLRequest aclRequest = new SetACLRequest(); aclRequest.Key = fileKey; aclRequest.BucketName = BucketName; S3AccessControlList aclList = new S3AccessControlList(); Owner owner = new Owner(); owner.Id = "oyesil"; owner.DisplayName = ""; aclList.Owner = owner; if (anonymouseReadAccess) { S3Grantee grantPublicRead = new S3Grantee(); grantPublicRead.URI = " http://acs.amazonaws.com/groups/global/AllUsers"; aclList.AddGrant(grantPublicRead, S3Permission.READ); } //Authenticated user read access S3Grantee grantAuthenticatedRead = new S3Grantee(); grantAuthenticatedRead.URI = " http://acs.amazonaws.com/groups/global/AuthenticatedUsers"; aclList.AddGrant(grantAuthenticatedRead, S3Permission.READ); aclRequest.ACL = aclList; Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(ConfigurationLibrary.Config.fileAmazonS3AccessKey, ConfigurationLibrary.Config.fileAmazonS3SecreyKey); SetACLResponse aclResponse = client.SetACL(aclRequest); client.Dispose(); }
private DataGridViewRow rowFromGrants(S3Grantee grantee, IEnumerable <S3Permission> permissions) { // Create the S3Grantee from the Canonical User ID, Email Address, or Group (whichever was provided) int grTypeIndex = 0; string grName = null; GranteeType type = grantee.Type; if (type == GranteeType.CanonicalUser) { grTypeIndex = 0; grName = grantee.CanonicalUser; } else if (type == GranteeType.Email) { grTypeIndex = 1; grName = grantee.EmailAddress; } else if (type == GranteeType.Group) { grTypeIndex = 2; grName = grantee.URI; } // Add a grant with READ permissions, if required bool canRead = permissions.Contains(S3Permission.READ); bool canReadAcl = permissions.Contains(S3Permission.READ_ACP); bool canWriteAcl = permissions.Contains(S3Permission.WRITE_ACP); // Add a grant with WRITE_ACP permissions, if required var row = DgvGrants.RowTemplate.Clone() as DataGridViewRow; string grType = DgvColGranteeType.Items[grTypeIndex].ToString(); row.CreateCells(DgvGrants, grType, grName, canRead, canReadAcl, canWriteAcl); return(row); }
static void WriteBucketAcl() { string id = Common.InputString("Bucket:", null, false); string owner = Common.InputString("Owner:", "default", false); PutACLRequest request = new PutACLRequest(); request.BucketName = id; request.AccessControlList = new S3AccessControlList(); request.AccessControlList.Owner = new Owner(); request.AccessControlList.Owner.DisplayName = owner; request.AccessControlList.Grants = new List <S3Grant>(); S3Grant grant = new S3Grant(); S3Grantee grantee = new S3Grantee(); grantee.CanonicalUser = owner; grantee.DisplayName = owner; grant.Grantee = grantee; request.AccessControlList.Grants.Add(grant); PutACLResponse response = _S3Client.PutACLAsync(request).Result; int statusCode = (int)response.HttpStatusCode; if (response != null) { Console.WriteLine("Success"); return; } else { Console.WriteLine("Failed"); return; } }
public IRequest Marshall(PutBucketLoggingRequest putBucketLoggingRequest) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected O, but got Unknown //IL_0303: Unknown result type (might be due to invalid IL or missing references) IRequest val = new DefaultRequest(putBucketLoggingRequest, "AmazonS3"); val.set_HttpMethod("PUT"); val.set_ResourcePath("/" + S3Transforms.ToStringValue(putBucketLoggingRequest.BucketName)); val.AddSubResource("logging"); StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture); using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Encoding = Encoding.UTF8, OmitXmlDeclaration = true })) { xmlWriter.WriteStartElement("BucketLoggingStatus", ""); S3BucketLoggingConfig loggingConfig = putBucketLoggingRequest.LoggingConfig; if (loggingConfig != null && loggingConfig != null) { S3BucketLoggingConfig s3BucketLoggingConfig = loggingConfig; if (s3BucketLoggingConfig != null && s3BucketLoggingConfig.IsSetTargetBucket()) { xmlWriter.WriteStartElement("LoggingEnabled", ""); xmlWriter.WriteElementString("TargetBucket", "", S3Transforms.ToXmlStringValue(s3BucketLoggingConfig.TargetBucketName)); List <S3Grant> grants = s3BucketLoggingConfig.Grants; if (grants != null && grants.Count > 0) { xmlWriter.WriteStartElement("TargetGrants", ""); foreach (S3Grant item in grants) { xmlWriter.WriteStartElement("Grant", ""); if (item != null) { S3Grantee grantee = item.Grantee; if (grantee != null) { xmlWriter.WriteStartElement("Grantee", ""); if (grantee.IsSetType()) { xmlWriter.WriteAttributeString("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance", ((object)grantee.Type).ToString()); } if (grantee.IsSetDisplayName()) { xmlWriter.WriteElementString("DisplayName", "", S3Transforms.ToXmlStringValue(grantee.DisplayName)); } if (grantee.IsSetEmailAddress()) { xmlWriter.WriteElementString("EmailAddress", "", S3Transforms.ToXmlStringValue(grantee.EmailAddress)); } if (grantee.IsSetCanonicalUser()) { xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(grantee.CanonicalUser)); } if (grantee.IsSetURI()) { xmlWriter.WriteElementString("URI", "", S3Transforms.ToXmlStringValue(grantee.URI)); } xmlWriter.WriteEndElement(); } if (item.IsSetPermission()) { xmlWriter.WriteElementString("Permission", "", S3Transforms.ToXmlStringValue(ConstantClass.op_Implicit(item.Permission))); } } xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); } if (s3BucketLoggingConfig.IsSetTargetPrefix()) { xmlWriter.WriteElementString("TargetPrefix", "", S3Transforms.ToXmlStringValue(s3BucketLoggingConfig.TargetPrefix)); } else { xmlWriter.WriteStartElement("TargetPrefix"); xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } try { string text = stringWriter.ToString(); val.set_Content(Encoding.UTF8.GetBytes(text)); val.get_Headers()["Content-Type"] = "application/xml"; string value = AmazonS3Util.GenerateChecksumForContent(text, fBase64Encode: true); val.get_Headers()["Content-MD5"] = value; return(val); } catch (EncoderFallbackException ex) { throw new AmazonServiceException("Unable to marshall request to XML", (Exception)ex); } }
private IEnumerable <S3Grant> grantsFromRow(DataGridViewRow row) { // Create the S3Grantee from the Canonical User ID, Email Address, or Group (whichever was provided) S3Grantee grantee = null; string typeStr = row.Cells[DgvColGranteeType.Index].Value.ToString(); string granteeStr = row.Cells[DgvColGrantee.Index].Value.ToString(); if (typeStr == "Canonical User ID") { grantee = new S3Grantee() { CanonicalUser = granteeStr } } ; else if (typeStr == "Email Address") { grantee = new S3Grantee() { EmailAddress = granteeStr } } ; else if (typeStr == "Group") { grantee = new S3Grantee() { URI = granteeStr } } ; IList <S3Grant> grants = new List <S3Grant>(3); // Add a grant with READ permissions, if required object canRead = row.Cells[DgvColCanRead.Index].Value; if (canRead != null && (bool)canRead) { grants.Add(new S3Grant() { Grantee = grantee, Permission = S3Permission.READ, }); } // Add a grant with READ_ACP permissions, if required object canReadAcl = row.Cells[DgvColCanReadAcl.Index].Value; if (canReadAcl != null && (bool)canReadAcl) { grants.Add(new S3Grant() { Grantee = grantee, Permission = S3Permission.READ_ACP, }); } // Add a grant with WRITE_ACP permissions, if required object canWriteAcl = row.Cells[DgvColCanWriteAcl.Index].Value; if (canWriteAcl != null && (bool)canWriteAcl) { grants.Add(new S3Grant() { Grantee = grantee, Permission = S3Permission.WRITE_ACP, }); } return(grants); }
public IRequest Marshall(PutACLRequest putObjectAclRequest) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected O, but got Unknown //IL_034a: Unknown result type (might be due to invalid IL or missing references) IRequest val = new DefaultRequest(putObjectAclRequest, "AmazonS3"); val.set_HttpMethod("PUT"); if (putObjectAclRequest.IsSetCannedACL()) { val.get_Headers().Add("x-amz-acl", S3Transforms.ToStringValue(ConstantClass.op_Implicit(putObjectAclRequest.CannedACL))); } val.set_ResourcePath(string.Format(CultureInfo.InvariantCulture, "/{0}/{1}", S3Transforms.ToStringValue(putObjectAclRequest.BucketName), S3Transforms.ToStringValue(putObjectAclRequest.Key))); val.AddSubResource("acl"); if (putObjectAclRequest.IsSetVersionId()) { val.AddSubResource("versionId", S3Transforms.ToStringValue(putObjectAclRequest.VersionId)); } StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture); using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Encoding = Encoding.UTF8, OmitXmlDeclaration = true })) { S3AccessControlList accessControlList = putObjectAclRequest.AccessControlList; if (accessControlList != null) { xmlWriter.WriteStartElement("AccessControlPolicy", ""); List <S3Grant> grants = accessControlList.Grants; if (grants != null && grants.Count > 0) { xmlWriter.WriteStartElement("AccessControlList", ""); foreach (S3Grant item in grants) { xmlWriter.WriteStartElement("Grant", ""); if (item != null) { S3Grantee grantee = item.Grantee; if (grantee != null) { xmlWriter.WriteStartElement("Grantee", ""); if (grantee.IsSetType()) { xmlWriter.WriteAttributeString("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance", ((object)grantee.Type).ToString()); } if (grantee.IsSetDisplayName()) { xmlWriter.WriteElementString("DisplayName", "", S3Transforms.ToXmlStringValue(grantee.DisplayName)); } if (grantee.IsSetEmailAddress()) { xmlWriter.WriteElementString("EmailAddress", "", S3Transforms.ToXmlStringValue(grantee.EmailAddress)); } if (grantee.IsSetCanonicalUser()) { xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(grantee.CanonicalUser)); } if (grantee.IsSetURI()) { xmlWriter.WriteElementString("URI", "", S3Transforms.ToXmlStringValue(grantee.URI)); } xmlWriter.WriteEndElement(); } if (item.IsSetPermission()) { xmlWriter.WriteElementString("Permission", "", S3Transforms.ToXmlStringValue(ConstantClass.op_Implicit(item.Permission))); } } xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); Owner owner = accessControlList.Owner; if (owner != null) { xmlWriter.WriteStartElement("Owner", ""); if (owner.IsSetDisplayName()) { xmlWriter.WriteElementString("DisplayName", "", S3Transforms.ToXmlStringValue(owner.DisplayName)); } if (owner.IsSetId()) { xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(owner.Id)); } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } try { string text = stringWriter.ToString(); val.set_Content(Encoding.UTF8.GetBytes(text)); val.get_Headers()["Content-Type"] = "application/xml"; string value = AmazonS3Util.GenerateChecksumForContent(text, fBase64Encode: true); val.get_Headers()["Content-MD5"] = value; return(val); } catch (EncoderFallbackException ex) { throw new AmazonServiceException("Unable to marshall request to XML", (Exception)ex); } }
public S3Grant(S3Grantee grantee, S3Permission permission) { Grantee = grantee; Permission = permission; }