Java Code Examples for com.aliyun.oss.OSSClient#getObject()

The following examples show how to use com.aliyun.oss.OSSClient#getObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AliyunOSSClientUtil.java    From xmfcn-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 创建模拟文件夹
 *
 * @param ossClient  oss连接
 * @param bucketName 存储空间
 * @param folder     模拟文件夹名如"qj_nanjing/"
 * @return 文件夹名
 */
public static String createFolder(OSSClient ossClient, String bucketName, String folder) {
    //文件夹名
    final String keySuffixWithSlash = folder;
    //判断文件夹是否存在,不存在则创建
    if (!ossClient.doesObjectExist(bucketName, keySuffixWithSlash)) {
        //创建文件夹
        ossClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
        logger.info("创建文件夹成功");
        //得到文件夹名
        OSSObject object = ossClient.getObject(bucketName, keySuffixWithSlash);
        String fileDir = object.getKey();
        return fileDir;
    }
    return keySuffixWithSlash;
}
 
Example 2
Source File: TestOSSService.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws OSSException, ClientException, IOException {
    OSSClient client = new OSSClient("oss-cn-hangzhou.aliyuncs.com", "", "", "");
    // BucketInfo info = client.getBucketInfo("edrmry");
    boolean exists = client.doesBucketExist("edrmry");
    System.out.println(exists);
    // System.out.println(client.listBuckets().size());
    // client.createBucket("dzh1");
    PutObjectResult r = client.putObject("edrmry", "dzh1.jpg", new FileInputStream("/Users/dzh/Pictures/8.pic.jpg"));
    System.out.println(r.getETag());
    OSSObject o = client.getObject("edrmry", "dzh1");
    InputStream is = o.getObjectContent();

    FileOutputStream fos = new FileOutputStream("/Users/dzh/Pictures/8.pic.2.jpg");
    int len = 0;
    byte[] buf = new byte[32];
    while ((len = is.read(buf)) != -1) {
        fos.write(buf, 0, len);
    }
    fos.flush();
    fos.close();
}
 
Example 3
Source File: ApiBootOssService.java    From api-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void download(String objectName, String localFile) throws ApiBootObjectStorageException {
    try {
        OSSClient ossClient = getOssClient();
        ossClient.getObject(new GetObjectRequest(bucketName, objectName).withProgressListener(new OssProgressListener(objectName, apiBootObjectStorageProgress)), new File(localFile));
        closeOssClient(ossClient);
    } catch (Exception e) {
        throw new ApiBootObjectStorageException(e.getMessage(), e);
    }
}
 
Example 4
Source File: AliyunOssUtils.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 同步 阿里云OSS 到本地
 *
 * @param path
 * @param toFile
 * @return
 */
public static boolean download(String path, File toFile) {
    boolean enable = JPressOptions.getAsBool(KEY_ENABLE);

    if (!enable || StrUtil.isBlank(path)) {
        return false;
    }

    path = removeFileSeparator(path);
    String ossBucketName = JPressOptions.get(KEY_BUCKETNAME);
    OSSClient ossClient = createOSSClient();
    try {

        if (!toFile.getParentFile().exists()) {
            toFile.getParentFile().mkdirs();
        }

        if (!toFile.exists()) {
            toFile.createNewFile();
        }
        ossClient.getObject(new GetObjectRequest(ossBucketName, path), toFile);
        return true;
    } catch (Throwable e) {
        log.error("aliyun oss download error!!!  path:" + path + "   toFile:" + toFile, e);
        if (toFile.exists()) {
            toFile.delete();
        }
        return false;
    } finally {
        ossClient.shutdown();
    }
}
 
Example 5
Source File: SimpleGetObjectSample.java    From albert with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    /*
     * Constructs a client instance with your account for accessing OSS
     */
    OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    try {
        
        /**
         * Note that there are two ways of uploading an object to your bucket, the one 
         * by specifying an input stream as content source, the other by specifying a file.
         */
        
        /*
         * Upload an object to your bucket from an input stream
         */
        System.out.println("Uploading a new object to OSS from an input stream\n");
        String content = "Thank you for using Aliyun Object Storage Service";
        client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        
        /*
         * Upload an object to your bucket from a file
         */
        System.out.println("Uploading a new object to OSS from a file\n");
        client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
        
        /*
         * 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());
        
    } 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();
    }
}