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

时间:2018-10-07 14:17:34

标签: java amazon-s3 sdk aws-sdk

我编写了一个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

问题似乎出在程序的第二部分,因为bucket列表正在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));
}
public static void main(String[] args) throws Exception
{
    final String accessKey = "XXXXXXXXXXXXXX";
    final String secretKey = "XXXXXXXXXXXXXXXXXXXXXXXX";
    BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    @SuppressWarnings("deprecation")
    final AmazonS3 s3 = new AmazonS3Client(credentials);
    S3ClientOptions opts = new S3ClientOptions().withPathStyleAccess(true);
    s3.setS3ClientOptions(opts);

    s3.setEndpoint("https://XXXXXX.com");
    ListBuckets.time("startTime");
    ListBuckets.listObjects(s3);
    //String pathName = "C:\\Users\\XXXXXX\\Documents\\New folder\\New Text Document - Copy.txt";
    String pathName = "/home/abcd/XXXXX/objectStorage/CHANGELOG.mdown";
    ListBuckets.putObject(s3, "snap-shot/sample-aws-ex", pathName, pathName);
    ListBuckets.time("end time");
}

}`

1 个答案:

答案 0 :(得分:0)

不可思议! 您知道linux的问题是什么吗?对象名和路径名是两个不同的东西。

putObject(AmazonS3 s3, String bucketName, String objectName, String pathName)

其中pathName是文件的路径,即

String pathName = "/home/abc/xxxxx/objectStorage/errorlog.txt";

请注意,它以正斜杠开头,而对象名称不应以/开头

String objectName = "home/abc/xxxxxx/objectStorage/errorlog.txt";

我希望抛出的异常会更好地说明问题所在。引发的异常仅使我偏离了根本原因。