I'm trying to upload a file with presigned AWS url.
I generate a presigned url like that :
use AsyncAws\S3\S3Client;
$s3 = new S3Client();
$bucket = 'my-bucket';
$key = 'myfile.pdf';
$date = new \DateTimeImmutable('+15minutes');
$contentType = 'application/pdf';
$input = new GetObjectRequest([
'Bucket' => $bucket,
'Key' => $key,
'ContentType' => $contentType
]);
$presignUrl = $s3->presign($input, $date);
This code above works fine, I get a presigned url like this : "https://my-bucket.s3.eu-west-3.amazonaws.com/myfile.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220809T141153Z&X-Amz-Expires=900&X-Amz-Credential=XXXXXXXXXXXXXXX%2F20220809%2Feu-west-3%2Fs3%2Faws4_request&x-amz-content-sha256=UNSIGNED-PAYLOAD&X-Amz-SignedHeaders=host&X-Amz-Signature=XXXXXXXXXXXXXXXXXXXXXXXXXX"
Next, I try to use this url to upload a file with Postman.
I select my pdf file via Body / binary / Select file
This is my postman config and response :
As you can see, I have a 403 code response.
These are my headers (of course I've tried with differents Content-Type but nothing better than the exactly same 403 response.
Important information : With the same $s3 instance and authentification, if I try to get object url ($content = $s3->getObject($input); with AsyncAws), it works well. So I suppose, it is not an authentification issue.
Thanks in advance if you have any idea for me !
trying to implement REST API oauth2.0 authentification with symfony4.
The road /oauth/v2/token works well but, i'm trying to create /api/login road that will get credentials from the request and make a Httpclient->request() to /oauth/v2/token and get the access token and return to the user.
This is my code :
public function login(Request $request)
{
//dd($request->getMethod());
$method = $request->getMethod();
$grantType = $request->query->get('grant_type');
$clientId = $request->query->get('client_id');
$secretId = $request->query->get('client_secret');
$username = $request->query->get('username');
$password = $request->query->get('password');
$path = $_SERVER['API_BASE_URL'] . 'oauth/v2/token';
$httpClient = HttpClient::create();
//dd($httpClient);
$response = $httpClient->request($method, $path, [
// these values are automatically encoded before including them in the URL
'query' => [
'grant_type' => $grantType,
'client_id' => $clientId,
'client_secret' => $secretId,
'username' => $username,
'password' => $password,
],
]);
$statusCode = $response->getStatusCode();
$contentType = $response->getHeaders()['content-type'][0];
$content = $response->getContent();
$content = $response->toArray();
return $content;
}
But i'm getting this error since and i'm unable to solve :
{"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"Reading from the response stream reached the inactivity timeout.","class":"Symfony\\Component\\HttpClient\\Exception\\TransportException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"D:\\vendor\\symfony\\http-client\\Chunk\\ErrorChunk.php","line":105,"args":[]},{"namespace":"Symfony\\Component\\HttpClient\\Chunk","short_class":"ErrorChunk","class":"Symfony\\Component\\HttpClient\\Chunk\\ErrorChunk","type":"->","function":"__destruct","file":"\\vendor\\symfony\\http-client\\Response\\CurlResponse.php","line":121,"args":[]},{"namespace":"Symfony\\Component\\HttpClient\\Response","short_class":"CurlResponse","class":"Symfony\\Component\\HttpClient\\Response\\CurlResponse","type":"::","function":"Symfony\\Component\\HttpClient\\Response\\{closure}","file":"\\vendor\\symfony\\http-client\\Response\\ResponseTrait.php","line":67,"args":[["object","Symfony\\Component\\HttpClient\\Response\\CurlResponse"]]},{"namespace":"Symfony\\Component\\HttpClient\\Response","short_class":"CurlResponse","class":"Symfony\\Component\\HttpClient\\Response\\CurlResponse","type":"->","function":"getStatusCode","file":"\\src\\Controller\\RestApiController.php","line":121,"args":[]},{"namespace":"App\\Controller","short_class":"RestApiController","class":"App\\Controller\\RestApiController","type":"->","function":"login","file":"D:\\Binghana\\new\\goldwin-website\\vendor\\symfony\\http-kernel\\HttpKernel.php","line":150,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handleRaw","file":"\\vendor\\symfony\\http-kernel\\HttpKernel.php","line":67,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["integer",1]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handle","file":"D:\\Binghana\\new\\goldwin-website\\vendor\\symfony\\http-kernel\\Kernel.php","line":198,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["integer",1],["boolean",true]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"Kernel","class":"Symfony\\Component\\HttpKernel\\Kernel","type":"->","function":"handle","file":"D:\\Binghana\\new\\goldwin-website\\public\\index.php","line":25,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"]]}]}]}}
I have tried updating httpclient version, even using Guzzle, same error.
I am using SignatureV4 aws authentication to authenticate my API call,
I am also using Guzzle/Client to call API.
By following these above, my API call works for GET method only and not working for POST method. For POST method call it show an error ( The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method )
My code :
use Aws\Credentials\CredentialProvider;
use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$request = new Request(
'POST',
'https://XXXXXXXXXXXXx.com/XXXXXx/XXXXXXx',
[
'body' => '{
"param1": "loream",
"param2": "4970",
"param3": "1",
"param4": "2.0",
"param5": "mL",
"param6": "kgs",
"param7": 0,
}'
]
);
$key['credentials'] = array(
'key' => 'XXXXXXXXXXXXXXX-access-key',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-secret-key'
);
$credentials = call_user_func(CredentialProvider::defaultProvider(
$key
));
// Construct a request signer
$signer = new SignatureV4('execute-api',
'us-east-1'
);
// Sign the request
$request = $signer->signRequest($request, $credentials);
// Send the request
try {
$response = (new Client)->send($request);
print_r($response);
}
catch (Exception $exception) {
$responseBody = $exception->getResponse()->getBody(true);
echo $responseBody;
}
$response = (new Client)->send($request);
$results = json_decode($response->getBody());
I need a script to take my text, convert it to speech using AWS Polly API and save it on my server as mp3 file.
Currently when I load a page a player appears and plays back the speech clip but no file is downloaded.
What am I missing?
require '../../../include/lib/aws/aws-autoloader.php';
// Creating Amazon Polly Client
use Aws\Polly\PollyClient;
$config = [
'version' => 'latest',
'region' => 'us-west-2', //region
'credentials' => [
'key' => 'MY_KEY',
'secret' => 'my_AWS_secret',
]];
$client = new PollyClient($config);
// Converting Text to Speech via Polly API
$args = [
'OutputFormat' => 'mp3',
'Text' => "<speak><prosody rate='medium'>My text goes here..</prosody></speak>",
'TextType' => 'ssml',
'VoiceId' => "Joanna",
];
$result = $client->synthesizeSpeech($args);
$resultData = $result->get('AudioStream')->getContents();
// Listening the text
$size = strlen($resultData); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-Transfer-Encoding:chunked');
header("Content-Type: audio/mpeg");
header("Accept-Ranges: 0-$length");
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
echo $resultData;
// Download the Text to Speech in MP3 Format
header('Content-length: ' . strlen($resultData));
header('Content-Disposition: attachment; filename="./myfile.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
echo $resultData;
Your sending 2 responses one after the other. The first will be processed the second most likely will be ignored.
Basically you cant send headers after you already sent content. Refactor your code so you only send 1 response per request.
simply save the output in a new file:
file_put_contents("output.mp3", $resultData);
{ file_put_contents("output.mp3", $resultData);
This command worked for me as I was able to download the file on my local desktop
I am uploading PDF's to AmazonS3 manually, using Panic Transmis and via a PHP script/API.
For some reason, some display in your browser, and some force download.
I have checked permission and can not seem to see any issues, Can anyone help explain how to make PDF's always display in browser ( unless the user specifies otherwise ).
I don't think it is as browser issue.
You need to change the Content-Type and Content-Disposition.
Content-Type: application/pdf;
Content-Disposition: inline;
Using the AWS S3 console, find the file and using the context menu (right click) select Properties then it's under Metadata.
Or change this programmatically:
http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/create_object
In companion with well's answer, here an example:
public function save($bucket, $name, $content, $options = [])
{
$this->s3->putObject([
'Bucket' => $bucket,
'Key' => $name,
'Body' => $content,
] + $options);
}
$this->bucket->save('my-bucket', 'SofiaLoren.pdf', $content, [
'ContentType' => 'application/pdf',
'ContentDisposition' => 'inline',
]);