AmazonS3Exception:仅在 linux 远程服务器中请求签名错误?

我编写了一个 Java 程序来列出所有存储桶并在 S3 兼容对象存储服务中上传文件。该程序在 Windows 我的本地机器上运行良好,但是当我(当然在更改要上传的文件的路径之后)在远程 linux 服务器中传输可运行的 jar 并执行它时,我收到以下错误 -


> Exception in thread "main"

> com.amazonaws.services.s3.model.AmazonS3Exception: The request

> signature we calculated does not match the signature you provided.

> Check your AWS Secret Access Key and signing method. For more

> information, see REST Authentication and SOAP Authentication for

> details. (Service: Amazon S3; Status Code: 403; Error Code:

> SignatureDoesNotMatch; Request ID:

> 4e271b5b-d7f5-42b3-a4ad-886988bcb785; S3 Extended Request ID: null),

> S3 Extended Request ID: null

问题似乎出在程序的第二部分,因为存储桶列表在 linux env 中返回。同样,但在文件上传过程中它抛出错误。


import java.io.File;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;


import com.amazonaws.auth.BasicAWSCredentials;

import com.amazonaws.services.s3.AmazonS3;

import com.amazonaws.services.s3.AmazonS3Client;

import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import com.amazonaws.services.s3.S3ClientOptions;

import com.amazonaws.auth.AWSCredentialsProvider;

import com.amazonaws.services.s3.model.Bucket;


/**

 * List your Amazon S3 buckets.

 */

public class ListBuckets

{

    private static void listObjects(AmazonS3 s3) {

        List<Bucket> buckets = s3.listBuckets();

        System.out.println("Your Amazon S3 buckets are:");

        for (Bucket b : buckets) {

            System.out.println("* " + b.getName());

        }

    }

    private static void putObject(AmazonS3 s3, String bucketName, String objectName, String pathName) throws Exception

{


    s3.putObject(bucketName, objectName, new File(pathName));



}

private static void time(String t) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    Date date = new Date();

    System.out.println(t+"-->"+dateFormat.format(date));

}



GCT1015
浏览 415回答 1
1回答

缥缈止盈

逆天!你知道 linux 的问题是什么吗?对象名和路径名是两个不同的东西。putObject(AmazonS3&nbsp;s3,&nbsp;String&nbsp;bucketName,&nbsp;String&nbsp;objectName,&nbsp;String&nbsp;pathName)其中 pathName 是文件的路径,即String&nbsp;pathName&nbsp;=&nbsp;"/home/abc/xxxxx/objectStorage/errorlog.txt";注意它以正斜杠开头,而对象名称不应以/ie开头String&nbsp;objectName&nbsp;=&nbsp;"home/abc/xxxxxx/objectStorage/errorlog.txt";我希望抛出的异常能够更清楚地说明出了什么问题。抛出的异常只会让我偏离根本原因。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java