Proper syntax for AWS S3 php-sdk "putObjectAcl" - php

I've been tasked with figuring out how the AWS PHP sdk works so we can possibly use it for hosting customer image data for our web server. I've been able to successfully test most of the functionality of creating, managing and loading data into a bucket, but when I try to view the contents I get 'access denied'.
Going into the management console, I figured out how to set the permissions so I could view the file, either with a specific host rule or by setting both the bucket and the object world-readable.
However, no matter how I try following the examples in the PHP sdk [limited] documentation, I cannot seem to set the ACL values using the php code provided by Amazon.
Their examples just list in the place of the various value and I have tried filling in the relevant data for my bucket, object and account in those and it doesn't work. I've tried doing a getObjectAcl and sending back something similar to what was received and it doesn't work. I've tried looking at examples on-line and what little I have found doesn't work.
Here's an example of the latest I have tried:
$params = [
'ACL' => 'public-read',
'AccessControlPolicy' => [
'Grants' => [
[
'Grantee' => [
'DisplayName' => 'Owner',
'ID' => $awsId,
'Type' => "CanonicalUser"
],
'Permission' => "FULL_CONTROL"
],
[
'Grantee' => [
'DisplayName' => 'All Users',
'URI' => "http://acs.amazonaws.com/groups/global/AllUsers",
'Type' => "Group"
],
'Permission' => "READ"
],
],
'Owner' => [
'ID' => $awsId
]
],
'Bucket' => "our-test-bucket",
'Key' => "800x600.jpg"
];
$result = $awsSdk->getS3Client()->putObjectAcl($params);
The resulting output:
Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with
message 'Error executing "PutObjectAcl" on
"https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl";
AWS HTTP error: Client error: PUT
https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl
resulted in /project/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php
on line 191
Aws\S3\Exception\S3Exception: Error executing "PutObjectAcl" on "https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl";
AWS HTTP error: Client error: PUT
https://our-test-bucket.s3.us-east-2.amazonaws.com/800x600.jpg?acl
resulted in a 400 Bad Request response:
MalformedACLErrorThe XML you provided was not well-f (truncated...)
MalformedACLError (client): The XML you provided was not well-formed or did not validate against our published schema -
MalformedACLErrorThe XML you provided was not well-formed or did not validate against our published
schemaB24661919936C2DADft/***********************************************= in /project/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php on line
191

I managed to drop a trace into the guzzle html messageTrait withHeader() function so I could watch the outgoing xml in the php stream. I then compared the result with examples of the xml I found in various places in a google search and through trial and error with the comparisons, I traced the problem down to the 'DisplayName' => 'All Users' in the Group Grant. Remove that line and it appears to work.
$pubAcl = [
'Owner' => [
'ID' => $awsId,
'DisplayName' => 'Owner'
],
'Grants' => [
[
'Grantee' => [
'ID' => $awsId,
'DisplayName' => 'Owner',
'Type' => "CanonicalUser"
],
'Permission' => "FULL_CONTROL"
],
[
'Grantee' => [
'URI' => "http://acs.amazonaws.com/groups/global/AllUsers",
'Type' => "Group"
],
'Permission' => "READ"
]
]
];
(The order change was the result of testing but didn't turn out to be the problem)

Related

Error "(get) unknown parameter: 'personFields'" when following examples in Google People API documentation

I am trying to use the Google People API to add and update my Google contacts. I have set up the API almost verbatim with the example code given in Google's documentation (https://developers.google.com/people/v1/getting-started). I get an error for the following line of code, which again comes verbatim from the documentation:
$profile = $people_service->people->get('people/me', array('personFields' => 'names,emailAddresses'));
The error is as follows:
Fatal error: Uncaught exception 'Google_Exception' with message '(get) unknown parameter: 'personFields'' in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Service/Resource.php:147 Stack trace: #0 /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/People/Resource/People.php(52): Google_Service_Resource->call('get', Array, 'Google_Service_...') #1 /home/danbak15/bakerlegalservicesmo.com/office/BLScontacts.php(36): Google_Service_People_Resource_People->get('people/me', Array) #2 {main} thrown in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Service/Resource.php on line 147
I have seen an answer to a similar question (Can't access my profile data when accessing google-people API) suggest using the Google_Service_PeopleService class instead of the Google_Service_People class as called for in the documentation. However, when I try to do this, I get another error:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } } ' in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php:118 Stack trace: #0 /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'G in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php on line 118
At this point, I am at a total loss. I would like to use Google Contacts to keep track of my contacts and would like to be able to work with Google Contacts via PHP so that I can add them to my database. Can anyone shed any light on this? Thank you in advance for any help!
UPDATE:
I tried moving the page to a local server on my computer (forgetting that Google redirects the page to my website) to see if I could get a different result from a different server. Somehow, magically, it worked when I tried to access the script from my website—for about 15-30 minutes or so. Then, I got the same error as before. If I run the page from my localhost server, I will get caught in an endless authorization redirect loop (to be expected given that Google redirects me to the online page), but then the online page will work for a time.
At this point, I can't begin to guess where the issue is. Does this make sense to anyone else?
This worked fine for me:
$scopes[] = 'https://www.googleapis.com/auth/contacts.readonly';
$client->setScopes($scopes);
$service = new Google_Service_People($client);
$optParams = ['requestMask.includeField' => 'person.names'];
$connections = $service->people_connections->listPeopleConnections('people/me', $optParams)->getConnections();
foreach ($connections as $connection) {
if (!empty($connection->getNames()[0])) {
print $connection->getNames()[0]->getDisplayName() . "\n" . '<br>';
}
}
I guess I know whats happens.
My English not very well so I hope you can understand what I mean..
In my case, I'm using Google_Service_People class (src\Google\Service\People.php) and get same error as you. So I take a look about People.php and see this :
$this->people = new Google_Service_People_Resource_People(
$this,
$this->serviceName,
'people',
array(
'methods' => array(
'get' => array(
'path' => 'v1/{+resourceName}',
'httpMethod' => 'GET',
'parameters' => array(
'resourceName' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'requestMask.includeField' => array( // <= This is the problem.
'location' => 'query',
'type' => 'string',
),
),
),
I check the official document and it say "requestMask.includeField is already DEPRECATED." but it still in api's code by somehow.
So I change it to this :
$this->people = new Google_Service_People_Resource_People(
$this,
$this->serviceName,
'people',
array(
'methods' => array(
'get' => array(
'path' => 'v1/{+resourceName}',
'httpMethod' => 'GET',
'parameters' => array(
'resourceName' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'personFields' => array( // <- Change to 'personFields'
'location' => 'query',
'type' => 'string',
),
),
After that every thing go well.
If you are using Google API Client Libraries from PHP. This client library is in beta. So there something in their api doesn't look like how it exactly is in their api document.
So that is my case. English not my language so I hope you can understand what I trying to describe.
John Rodriguez solved the issue. Thank you! My library is a little bit different, but the problem was the same. In my case I am using the Google_Service_PeopleService class instead of the Google_Service_People class. The path to the file I changed is: google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/PeopleService.php. In this case, $this->people is a Google_Service_PeopleService_Resource_People class which contains an array of methods. I commented out the depreciated code. Here is the relevant code:
$this->people = new Google_Service_PeopleService_Resource_People(
$this,
$this->serviceName,
'people',
array(
'methods' => array(
'createContact' => array(
...
),
),
),'deleteContact' => array(
...
),
),
),'get' => array(
'path' => 'v1/{+resourceName}',
'httpMethod' => 'GET',
'parameters' => array(
'resourceName' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'personFields' => array(
'location' => 'query',
'type' => 'string',
),
/* 'requestMask.includeField' => array(
'location' => 'query',
'type' => 'string',
), */
),
),'getBatchGet' => array(
'path' => 'v1/people:batchGet',
'httpMethod' => 'GET',
'parameters' => array(
'personFields' => array(
'location' => 'query',
'type' => 'string',
),
/* 'requestMask.includeField' => array(
'location' => 'query',
'type' => 'string',
), */
'resourceNames' => array(
'location' => 'query',
'type' => 'string',
'repeated' => true,
),
),
),'updateContact' => array(
...
),
),
),
So far this has worked as it should, but I have not thoroughly tested it yet. Thank you for your help!

Using Guzzle with GetResponse API to save custom field?

I am sending a post request to the GetResponse API. Everything works fine until I add a custom field (customFieldValues) to save along with my new email contact.
$body_data =
[
'name' => $input['name'],
'email' => $input['email'],
'campaign' => [
'campaignId' => $campaign_id
],
'customFieldValues' => ['customFieldId' => 'LDe0h', 'value' => ['Save this test string.'] ]
];
When I send the request I get the following error message:
"errorDescription": "CustomFieldValue entry must be specified as array"
I have tried a few things now and not sure how to format this properly to have the API accept it.
Reference link:
http://apidocs.getresponse.com/v3/case-study/adding-contacts
I found the solution on github in an example for their php api here:
https://github.com/GetResponse/getresponse-api-php
I suppose I had to wrap an array inside an array inside of an array...geez:
'customFieldValues' => array(
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
)),
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
))
)
For GetResponse V3
'customFieldValues' => [
'customFieldId' => 'custom_field_id_from_API',
'name' => 'Website',
'value' => ['https://scholarshipspsy.com']
]
Please note the 'name' field is optional. The site sampled is an international scholarship platform.

ZF2 - Routing with encoded input parameters

I have the following issue which I do not know how to resolve.
I have the following route on a api I am using :
'router' => [
'routes' => [
'test' => [
'type' => 'regex',
'options' => [
'regex' => '/test(?<json>\/.*)?',
'defaults' => [
'controller' => TestController::class,
'action' => 'checkJson',
'json' => '',
],
'spec' => '/path%path%'
],
],
],
],
The idea is to catch all requests ending in a json string /test/[{"this is an example":1}]. This really works but the problem is, I would also like to accept when the input gets encoded like :
/test/%5B%7B%22this%20is%20an%20example%22%3A1%7D%5D
Any idea of how can I achieve what I am expecting ?
When the request is made using an encoded url, I get the following error in the browser :
Not Found
The requested URL /stock//test/[{"this is an example":1}] was not found on this server.
Any idea of how can I resolve this easily ?
Thank you

Use Amazon SNS to send SMS messages using PHP AWS SDK v2?

I inherited a PHP project and it's heavily integrated with AWS SDK v2. Using v3 is not an option at this time.
My question is how can I leverage SNS to send text messages to specific numbers as needed? That is, I don't want to send mass notifications to a bunch of phone numbers subscribed to a specific topic when an action occurs, I want to send a notification to a specific phone number when an action occurs.
I came across this https://stackoverflow.com/a/41268045/664881 but it appears to be using v3 of the AWS SDK. Is there an equivalent with v2 of the AWS SDK?
I managed to make it work in PHP AWS SDK v2, but you need to add new parameter in the source code.
// update file: aws-sdk-php/src/Aws/Sdk/Resources/sns-2010-03-31.php
'Publish' => array(
'parameters' => array(
'PhoneNumber' => array( // new parameter
'type' => 'string',
'location' => 'aws.query',
),
),
),
// You just need to publish it and include the `PhoneNumber` parameter
$snsClientResult = $snsClient->publish([
'Message' => 'YOUR_MESSAGE',
'PhoneNumber' => 'PHONE_NUMBER',
'MessageStructure' => 'SMS',
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'SENDER_ID',
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Promotional', // Transactional
]
]
]);
// Get the response
$snsClientResult['MessageId']

posting data into multiple queue aws services

I working with Queue Component for Yii2 from reference https://libraries.io/github/cybercog/yii2-queue
I wants to know that how can i post data into 2-3 different queue url for different feature
for that i have to configure 2-3 user here in below configuration code plsease let me know how can i provide multiple url in configuration
'components' => [
'queue' => [
'class' => 'UrbanIndo\Yii2\Queue\SqsQueue',
'module' => 'task',
'url' => 'https://sqs.ap-southeast-1.amazonaws.com/123456789012/queue',
'config' => [
'key' => 'AKIA1234567890123456',
'secret' => '1234567890123456789012345678901234567890',
'region' => 'ap-southeast-1',
],
]]
or how can i set url while posting data into queue
Yii::$app->queue->post(new Job(['route' => ['sync' => 'emailSync'], 'data' => $options]));

Categories