开发者社区> 问答> 正文

初次使用OSSInvalidBucketName出错



一下官方给出的例子我是copy过来的,只改了endpoint ,accessKeyId ,accessKeySecret ,bucketName ,但运行出错了,

public class GetStartedSample  {


     private static String endpoint = “自己的域名";    


        private static String accessKeyId = "自己的accessKeyId ";
        private static String accessKeySecret = "自己的accessKeyaccessKeySecret ";
        


        private static OSSClient client = null;


        public static void main(String[] args) throws IOException {
            /*
             * Constructs a client instance with your account for accessing OSS
             */
            client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            
            
            


            String bucketName = "fentest";//+ UUID.randomUUID();
            String key = "MyObjectKey";


            System.out.println("===========================================");
            System.out.println("Getting Started with OSS SDK for Java");
            System.out.println("===========================================\n");


            try {
                /*
                 * Create a new OSS bucket
                 */
                System.out.println("Creating bucket " + bucketName + "\n");
                client.createBucket(bucketName);


                /*
                 * Determine whether the newly bucket exists
                 */
                boolean exists = client.doesBucketExist(bucketName);
                System.out.println("Does bucket " + bucketName + " exist? " + exists + "\n");


                /*
                 * 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
                 */
                System.out.println("Uploading a new object to OSS from a file\n");
                client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));


                /*
                 * Determine whether an object residents in your bucket
                 */
                exists = client.doesObjectExist(bucketName, key);
                System.out.println("Does object " + bucketName + " exist? " + exists + "\n");


                /*
                 * Download an object from your bucket
                 */
                System.out.println("Downloading an object");
                OSSObject 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
                 */
                System.out.println("Listing objects");
                ObjectListing objectListing = client.listObjects(new ListObjectsRequest(bucketName)
                        .withPrefix("My"));
                for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                    System.out.println(" - " + objectSummary.getKey() + "  " +
                                       "(size = " + objectSummary.getSize() + ")");
                }
                System.out.println();


                /*
                 * Delete an object
                 */
                System.out.println("Deleting an object\n");
                client.deleteObject(bucketName, key);


                /*
                 * Delete a bucket
                 */
                System.out.println("Deleting bucket " + bucketName + "\n");
                client.deleteBucket(bucketName);
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message: " + oe.getErrorCode());
                System.out.println("Error Code:       " + oe.getErrorCode());
                System.out.println("Request ID:      " + oe.getRequestId());
                System.out.println("Host ID:           " + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message: " + ce.getMessage());
            } finally {
                /*
                 * Do not forget to shut down the client finally to release all allocated resources.
                 */
                client.shutdown();
            }
        }


        private static File createSampleFile() throws IOException {
            File file = File.createTempFile("oss-java-sdk-", ".txt");
            file.deleteOnExit();


            Writer writer = new OutputStreamWriter(new FileOutputStream(file));
            writer.write("abcdefghijklmnopqrstuvwxyz\n");
            writer.write("0123456789011234567890\n");
            writer.close();


            return file;
        }


        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();


            reader.close();
        }
        
        
运行后就出错了:
Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason.
Error Message: InvalidBucketName
Error Code:       InvalidBucketName
Request ID:      568B212C957EEB8871CEC278
Host ID:           这个不写了


我的BucketName是fentest,client = new OSSClient(endpoint, accessKeyId, accessKeySecret);这一步是没出错的
符合这个要求的啊,真的百思不得其解,希望高手们解答一下,万分感激了

  • Bucket命名规范只能包括小写字母,数字和短横线(-)
  • 必须以小写字母或者数字开头
  • 长度必须在3-63字节之间


展开
收起
vs大头妹 2016-01-05 10:04:18 19709 0
4 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
十分钟上线-使用函数计算构建支付宝小程序服务 立即下载
小程序 大世界 立即下载
《15分钟打造你自己的小程序》 立即下载