Sample code for 30+ languages & platforms
PowerBuilder

S3 Put Bucket Policy

See more Amazon S3 (new) Examples

Demonstrates how to add or replace a policy on a bucket. The following information is quoted from the AWS REST API reference documentation at PUT Bucket Policy
This implementation of the PUT operation uses the policy subresource to add to or replace a policy on a bucket. If the bucket already has a policy, the one in this request completely replaces it. To perform this operation, you must be the bucket owner. If you are not the bucket owner but have PutBucketPolicy permissions on the bucket, Amazon S3 returns a 405 Method Not Allowed. In all other cases for a PUT bucket policy request that is not from the bucket owner, Amazon S3 returns 403 Access Denied. There are restrictions about who can create bucket policies and which objects in a bucket they can apply to. For more information, go to Using Bucket Policies.

Important: This example requires Chilkat v9.5.0.66 or greater.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_AuthAws
oleobject loo_Json
string ls_ResponseJsonStr

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Important: This example requires Chilkat v9.5.0.66 or greater.

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Connect to the Amazon AWS REST server in the desired region.
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("s3-us-west-2.amazonaws.com",li_Port,li_BTls,li_BAutoReconnect)

// Provide AWS credentials.
loo_AuthAws = create oleobject
li_rc = loo_AuthAws.ConnectToNewObject("Chilkat.AuthAws")

loo_AuthAws.AccessKey = "AWS_ACCESS_KEY"
loo_AuthAws.SecretKey = "AWS_SECRET_KEY"
loo_AuthAws.ServiceName = "s3"

// This particular bucket is in the Oregon region, as opposed to the US Standard,
// therefore the Region must be set appropriately.
// Also note that we connected to "s3-us-west-2.amazonaws.com".
loo_AuthAws.Region = "us-west-2"

li_Success = loo_Rest.SetAuthAws(loo_AuthAws)

// Note: The above REST connection and setup of the AWS credentials
// can be done once.  After connecting, any number of REST calls can be made.
// The "auto reconnect" property passed to rest.Connect indicates that if
// the connection is lost, a REST method call will automatically reconnect
// if needed.
// --------------------------------------------------------------------------

// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkat.ocean".
// Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-west-2.amazonaws.com"
loo_Rest.Host = "chilkat.ocean.s3.amazonaws.com"

// Build the S3 JSON Policy to be sent in the request:
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("Version","2012-10-17")
loo_Json.UpdateString("Statement[0].Action[0]","s3:GetObject")
loo_Json.UpdateString("Statement[0].Effect","Allow")
loo_Json.UpdateString("Statement[0].Resource","arn:aws:s3:::chilkat.ocean/")
loo_Json.UpdateString("Statement[0].Principal","*")

// The JSON Policy constructed by the above lines of code is:
// (The bucket name is "chilkat.ocean")

// 	{ 
// 	  "Version": "2012-10-17",
// 	  "Statement": [
// 	    { 
// 	      "Action": [
// 	        "s3:GetObject"
// 	      ],
// 	      "Effect": "Allow",
// 	      "Resource": "arn:aws:s3:::chilkat.ocean/",
// 	      "Principal": "*"
// 	    }
// 	  ]
// 	}

// Add or replace the policy on the bucket.
ls_ResponseJsonStr = loo_Rest.FullRequestString("PUT","/?policy",loo_Json.Emit())
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_AuthAws
    destroy loo_Json
    return
end if

// When successful, the S3 Storage service will respond with a 200 or 204 response code,
// with an XML body.  
if (loo_Rest.ResponseStatusCode <> 200) AND (loo_Rest.ResponseStatusCode <> 204) then
    // Examine the request/response to see what happened.
    Write-Debug "response status code = " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "response status text = " + loo_Rest.ResponseStatusText
    Write-Debug "response header: " + loo_Rest.ResponseHeader
    Write-Debug "response body: " + ls_ResponseJsonStr
    Write-Debug "---"
    Write-Debug "LastRequestStartLine: " + loo_Rest.LastRequestStartLine
    Write-Debug "LastRequestHeader: " + loo_Rest.LastRequestHeader
end if

Write-Debug "Success."

// A successful response has no response body..
// (The Amazon documentation indicates a 204 response, but in our testing we receive a 200 response..)

// 	HTTP/1.1 204 No Content  
// 	x-amz-id-2: Uuag1LuByR5Onimru9SAMPLEAtRPfTaOFg==  
// 	x-amz-request-id: 656c76696e6727732SAMPLE7374  
// 	Date: Tue, 04 Apr 2010 20:34:56 GMT  
// 	Connection: keep-alive  
// 	Server: AmazonS3  


destroy loo_Rest
destroy loo_AuthAws
destroy loo_Json