1

我正在使用 Amazon 的 S3 Java 客户端来访问 Swisscom 的动态存储以及S3 Ninja

使用 S3 Ninja(即在本地主机上)创建存储桶和放置对象。创建存储桶似乎可以在云中使用,但放置对象却不行:

10 月 30 日 12:04:44 SquaccDevLog 5751ff25-6661-43d6-92ba-91b7c0cf7c55/[App/0]:com.amazonaws.services.s3.model.AmazonS3Exception:访问被拒绝(服务:Amazon S3;状态代码:403;错误代码:AccessDenied;请求 ID:空)

我可能遗漏了一些东西https://docs.developer.swisscom.com/services/offerings/dynamic.html#authentication - 但是什么(参见下面代码中的静态初始化程序)?

谢谢,保罗

PS:下面的代码大部分(即main和以下)是亚马逊的 S3 Java 示例。

/*
 * The MIT License
 *
 * Copyright (c) 2015 Squeng AG
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package models.s3;

import java.io.*;
import java.util.*;

import play.libs.*;

import com.amazonaws.*;
import com.amazonaws.auth.*;
import com.amazonaws.services.s3.*;
import com.amazonaws.services.s3.model.*;
import com.fasterxml.jackson.databind.node.*;


/**
 * Copyright © 2015 Squeng AG
 *
 * @author Paul
 */
public final class S3Factory
{
    private S3Factory()
    {
        // disable instance construction
    }


    // FIXME: move to configuration eventually, one for development, testing, and production each
    private static final AmazonS3 client;
    static {
        String vss = System.getenv( "VCAP_SERVICES" );
        if ( vss != null ) {
            ClientConfiguration clientConfig = new ClientConfiguration();
            clientConfig.setProtocol( Protocol.HTTPS );
            ObjectNode VCAP_SERVICES = (ObjectNode)Json.parse( vss );
            ObjectNode dynstrg = (ObjectNode)VCAP_SERVICES.get( "dynstrg" ).get( 0 );
            ObjectNode credentials = (ObjectNode)dynstrg.get( "credentials" );
            BasicAWSCredentials awsCreds = new BasicAWSCredentials( credentials.get( "accessKey" ).asText(), credentials.get( "sharedSecret" ).asText() );
            client = new AmazonS3Client( awsCreds, clientConfig );
            client.setEndpoint( credentials.get( "accessHost" ).asText() );
            client.setS3ClientOptions( (new S3ClientOptions()).withPathStyleAccess( true ) );
        } else {
            ClientConfiguration clientConfig = new ClientConfiguration();
            clientConfig.setProtocol( Protocol.HTTP );
            BasicAWSCredentials awsCreds = new BasicAWSCredentials( "AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" );
            client = new AmazonS3Client( awsCreds, clientConfig );
            client.setEndpoint( "localhost:9444/s3" );
            client.setS3ClientOptions( (new S3ClientOptions()).withPathStyleAccess( true ) );
        }
    }


    public static AmazonS3 createBucketIfNeedBe( String bucket )
    {
        if ( !client.doesBucketExist( Objects.requireNonNull( bucket ) ) ) {
            // FIXME: barely good enough for only one JVM
            synchronized ( bucket.intern() ) {
                if ( !client.doesBucketExist( bucket ) ) {
                    client.createBucket( bucket );
                }
            }
        }

        return client;
    }

    public static void main( String[] args )
        throws IOException
    {
        String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
        String key = "MyObjectKey";

        System.out.println( "===========================================" );
        System.out.println( "Getting Started with Amazon S3" );
        System.out.println( "===========================================\n" );

        try {
            /*
             * Create a new S3 bucket - Amazon S3 bucket names are globally unique, so once a bucket name has been taken by any user, you can't create another bucket with that same name.
             * 
             * You can optionally specify a location for your bucket if you want to keep your data closer to your applications or users.
             */
            System.out.println( "Creating bucket " + bucketName + "\n" );
            client.createBucket( bucketName );

            /*
             * List the buckets in your account
             */
            //            System.out.println( "Listing buckets" );
            //            for ( Bucket bucket : client.listBuckets() ) {
            //                System.out.println( " - " + bucket.getName() );
            //            }
            //            System.out.println();

            /*
             * Upload an object to your bucket - You can easily upload a file to S3, or upload directly an InputStream if you know the length of the data in the stream. You can also specify your own metadata when uploading to S3, which allows you set a
             * variety of options like content-type and content-encoding, plus additional metadata specific to your applications.
             */
            System.out.println( "Uploading a new object to S3 from a file\n" );
            client.putObject( new PutObjectRequest( bucketName, key, createSampleFile() ) );

            /*
             * Download an object - When you download an object, you get all of the object's metadata and a stream from which to read the contents. It's important to read the contents of the stream as quickly as possibly since the data is streamed directly
             * from Amazon S3 and your network connection will remain open until you read all the data or close the input stream.
             * 
             * GetObjectRequest also supports several other options, including conditional downloading of objects based on modification times, ETags, and selectively downloading a range of an object.
             */
            System.out.println( "Downloading an object" );
            S3Object object = client.getObject( new GetObjectRequest( bucketName, key ) );
            System.out.println( "Content-Type: " + object.getObjectMetadata().getContentType() );
            displayTextInputStream( object.getObjectContent() );

            /*
             * List objects in your bucket by prefix - There are many options for listing the objects in your bucket. Keep in mind that buckets with many objects might truncate their results when listing their objects, so be sure to check if the returned
             * object listing is truncated, and use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve additional results.
             */
            //            System.out.println( "Listing objects" );
            //            ObjectListing objectListing = client.listObjects( new ListObjectsRequest().withBucketName( bucketName ).withPrefix( "My" ) );
            //            for ( S3ObjectSummary objectSummary : objectListing.getObjectSummaries() ) {
            //                System.out.println( " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")" );
            //            }
            //            System.out.println();

            try ( Scanner s = new Scanner( System.in ) ) {
                System.out.println( "Unter data/s3 sollten das Bucket und das Object nun ersichtlich sein." );
                System.out.println( "Nach dem Drücken der Enter/Return-Taste werden sie wieder gelöscht ..." );
                s.nextLine();
            }

            /*
             * Delete an object - Unless versioning has been turned on for your bucket, there is no way to undelete an object, so use caution when deleting objects.
             */
            System.out.println( "Deleting an object\n" );
            client.deleteObject( bucketName, key );

            /*
             * Delete a bucket - A bucket must be completely empty before it can be deleted, so remember to delete any objects from your buckets before you try to delete them.
             */
            System.out.println( "Deleting bucket " + bucketName + "\n" );
            client.deleteBucket( bucketName );
        } catch ( AmazonServiceException ase ) {
            System.out.println( "Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason." );
            System.out.println( "Error Message:    " + ase.getMessage() );
            System.out.println( "HTTP Status Code: " + ase.getStatusCode() );
            System.out.println( "AWS Error Code:   " + ase.getErrorCode() );
            System.out.println( "Error Type:       " + ase.getErrorType() );
            System.out.println( "Request ID:       " + ase.getRequestId() );
            ase.printStackTrace( System.out );
        } catch ( AmazonClientException ace ) {
            System.out.println( "Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network." );
            System.out.println( "Error Message: " + ace.getMessage() );
            ace.printStackTrace( System.out );
        }
    }

    /**
     * Creates a temporary file with text data to demonstrate uploading a file to Amazon S3
     *
     * @return A newly created temporary file with text data.
     *
     * @throws IOException
     */
    private static File createSampleFile()
        throws IOException
    {
        File file = File.createTempFile( "aws-java-sdk-", ".txt" );
        file.deleteOnExit();

        Writer writer = new OutputStreamWriter( new FileOutputStream( file ) );
        writer.write( "abcdefghijklmnopqrstuvwxyz\n" );
        writer.write( "01234567890112345678901234\n" );
        writer.write( "!@#$%^&*()-=[]{};':',.<>/?\n" );
        writer.write( "01234567890112345678901234\n" );
        writer.write( "abcdefghijklmnopqrstuvwxyz\n" );
        writer.close();

        return file;
    }

    /**
     * Displays the contents of the specified input stream as text.
     *
     * @param input
     *            The input stream to display as text.
     *
     * @throws IOException
     */
    private static void displayTextInputStream( InputStream input )
        throws IOException
    {
        BufferedReader reader = new BufferedReader( new InputStreamReader( input ) );
        while ( true ) {
            String line = reader.readLine();
            if ( line == null )
                break;

            System.out.println( "    " + line );
        }
        System.out.println();
    }
}
4

1 回答 1

1

您是否检查了用于访问 Swisscom 对象存储的“PathStyleAccess”?大多数兼容的 S3 接口都需要此标志:

这似乎对我有用;这里有一些片段:

    S3ClientOptions options = new S3ClientOptions();
    options.setPathStyleAccess(true);

    AmazonS3Client client = new AmazonS3Client(new BasicAWSCredentials(uid, secret));
    client.setEndpoint("https://ds31s3.swisscom.com");
    client.setS3ClientOptions(options);


    /*
     * Creating a new bucket in the 
     */
    String newBucket = "mybucket";
    client.createBucket(newBucket, "Standard");

    /*
     * Add a new object
     */
    client.putObject(newBucket, "CloudFoundry1.png", objectFile);
    client.putObject(newBucket, "CloudFoundry2.png", objectFile);

    /*
     * List existing objects
     */
    ObjectListing objects = client.listObjects("mybucket");
    for (S3ObjectSummary summary : objects.getObjectSummaries()) {
        System.out.println(summary.getKey()+ "   "+summary.getOwner());
    }       

希望这可以帮助;

最佳马可

于 2015-12-07T17:51:19.347 回答