php 提供的令牌格式不正确或无效

mutmk8jj  于 2023-04-04  发布在  PHP
关注(0)|答案(4)|浏览(174)

显示latestAll S3 bucket文件,但当我上传文件时,会生成错误。
我有ARN和示例配置文件。

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;
}

消息:在error file url here上执行“PutObject”时出错;AWS HTTP错误:客户端错误:PUT error file url here导致400 Bad Request`响应:InvalidToken提供的令牌格式不正确或其他(截断...)InvalidToken(客户端):提供的令牌格式不正确或无效。-InvalidToken提供的令牌格式不正确或无效。

yc0p9oo0

yc0p9oo01#

在我的情况下。我正在尝试通过CLI。
1.我通过运行以下命令配置AWS CLI

aws configure

1.在~/.aws/credentials中,查找session_token。如果您有session_token,则更新它。否则删除session_token并重试。
瞧,它起作用了!

lfapxunr

lfapxunr2#

您收到此错误是因为默认情况下未启用香港(ap-east-1)。如果您具有正确的权限,则需要在AWS控制台中启用它,然后重试。
请参阅AWS文档,了解如何执行此操作。

jm2pwxwz

jm2pwxwz3#

我使用s3cmd,基于above answer,我能够通过删除.s3cmd文件并重新配置(s3cmd --configure)来解决,这清除了access_token

diff .s3cfg previous_s3cmd

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

还增加了块大小:

< recv_chunk = 65536
---
> recv_chunk = 4096

< send_chunk = 65536
---
> send_chunk = 4096

我想我以前选择了https请求,而不是(当前)默认的:

< use_https = True
---
> use_https = False

成功

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

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

0qx6xfy64#

在我的情况下,我不得不更新aws配置文件。你可以按照以下步骤。在本地命令行终端:
open ~./aws
打开凭据文件并更新以下3个条目的值:
aws访问密钥标识
aws秘密访问密钥
aws会话令牌
您可以从AWS控制台获取这些值。希望它有帮助!!

相关问题