The provided token is malformed or otherwise invalid

Viewed 21790

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.

4 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!

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.

Thank you! Your suggestion made me solve a problem I had been having for days. The Milan region eu-south-1 is also not enabled by default. If you try to use the STS assumeRole function without specifying the endpoint you are returned a token that is valid only for regions enabled by default. To use an STS token in a region that is not enabled by default you must use a local STS endpoint.

I am using s3cmd and based on the above answer I was able to solve by deleting the .s3cmd file and reconfiguring (s3cmd --configure), which cleared the access_token:

diff .s3cfg previous_s3cmd

< access_token =
---
> access_token = vkp1Jf\etc...

Also increased the chunk sizes:

< recv_chunk = 65536
---
> recv_chunk = 4096

< send_chunk = 65536
---
> send_chunk = 4096

And I guess I had previously opted for https requests, as opposed to the (current) default:

< use_https = True
---
> use_https = False

Success

Success. Your access key and secret key worked fine :-)

Now verifying that encryption works...
Success. Encryption and decryption worked fine :-)
Related