Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The provided token is malformed or otherwise invalid

latestAll S3 bucket file is displayed, but when I upload file then error is generate.

I have ARN and instance profile.

use Aws\Credentials\CredentialProvider;
use Aws\Credentials\InstanceProfileProvider;
use Aws\Credentials\AssumeRoleCredentialProvider;
use Aws\S3\S3Client;
use Aws\Sts\StsClient;

$profile = new InstanceProfileProvider();
$ARN = ""; // MY ARN
$sessionName = "s3-access-example";

$assumeRoleCredentials = new AssumeRoleCredentialProvider([
    'client' => new StsClient([
        'region' => "ap-east-1",
        'version' => "latest",
        'credentials' => $profile
    ]),
    'assume_role_params' => [
        'RoleArn' => $ARN,
        'RoleSessionName' => $sessionName,
    ],
]);

$provider = CredentialProvider::memoize($assumeRoleCredentials);

$this->s3hd = S3Client::factory([
    'credentials' => $provider,
    'version' => "latest",
    'region' => "ap-east-1"
]);

public function upload($name, $file, $type, $Bucket = false)
{
    if (! $Bucket) {
        $Bucket = $this->bucket;
    }
    $result = $this->s3hd->putObject([
        'Bucket' => $Bucket,
        'Key' => $name,
        'SourceFile' => $file,
        'ContentType' => $type,
        'ACL' => 'public-read'
    ]);
    $this->s3hd->waitUntil('ObjectExists', [
        'Bucket' => $Bucket,
        'Key' => $name
    ]);
    return $result;
}

Message: Error executing "PutObject" on error file url here; AWS HTTP error: Client error: PUT error file url here resulted in a 400 Bad Request` response: InvalidTokenThe provided token is malformed or other (truncated...) InvalidToken (client): The provided token is malformed or otherwise invalid. - InvalidTokenThe provided token is malformed or otherwise invalid.

like image 987
WO Developers Avatar asked Jun 11 '19 11:06

WO Developers


2 Answers

In my case. I was trying through CLI.

  1. I configured AWS cli by running
aws configure
  1. Inside ~/.aws/credentials, look for session_token. If you have the session_token. Then update it. Otherwise remove session_token and try again.

And Voila It worked!

like image 153
Santosh Kumar Avatar answered Nov 04 '22 16:11

Santosh Kumar


You're getting this error because Hong Kong (ap-east-1) is not enabled by default. You need to enable it in the AWS console if you have the correct permissions, then try again.

See the AWS Docs for instructions on how to do that.

like image 1
bildungsroman Avatar answered Nov 04 '22 16:11

bildungsroman