当前位置: 首页>>代码示例>>Java>>正文


Java OSSClient类代码示例

本文整理汇总了Java中com.aliyun.oss.OSSClient的典型用法代码示例。如果您正苦于以下问题:Java OSSClient类的具体用法?Java OSSClient怎么用?Java OSSClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


OSSClient类属于com.aliyun.oss包,在下文中一共展示了OSSClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: doPost

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static String doPost(String id, MultipartFile multipartFile) throws IOException{

        OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
        long currentTime = System.currentTimeMillis();
        String filename = id + String.valueOf(currentTime)+".png";

        //创建缓存文件
        File f = null;

        f = File.createTempFile("tmp", null);
        multipartFile.transferTo(f);

        //缓存文件上传至OSS
        InputStream inputStream = new FileInputStream(f);
        ossClient.putObject("zxbangban", filename, inputStream);
        //关闭OSS实例,删除临时文件
        ossClient.shutdown();
        f.deleteOnExit();
        return filename;

    }
 
开发者ID:zxbangban,项目名称:zxbangban,代码行数:22,代码来源:AliyunOSSUtil.java

示例2: putObject

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
/**
 * 上传Object
 *
 * @param client
 * @param bucketName
 * @param key
 * @param filePath
 * @return
 * @throws FileNotFoundException
 */
public static PutObjectResult putObject(OSSClient client, String bucketName, String key, String filePath) throws FileNotFoundException {

	// 获取指定文件的输入流
	File file = new File(filePath);
	InputStream content = new FileInputStream(file);

	// 创建上传Object的Metadata
	ObjectMetadata meta = new ObjectMetadata();

	// 必须设置ContentLength
	meta.setContentLength(file.length());
	// 用户自定义文件名称
	meta.addUserMetadata("filename", key);

	// 上传Object.
	PutObjectResult result = client.putObject(bucketName, key, content, meta);

	return result;
}
 
开发者ID:funtl,项目名称:framework,代码行数:30,代码来源:ObjectService.java

示例3: token

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
@GetMapping("/ali_token/{type}")
public Map<String,Object> token(@PathVariable Integer type){
    Map<String, Object> respMap = new LinkedHashMap<>();

    String host = "http://"+OSSInfo.BUCKET+"."+OSSInfo.END_POINT;

    OSSClient client = new OSSClient(OSSInfo.END_POINT,OSSInfo.ACCESS_ID,OSSInfo.ACCESS_KEY);

    try {
        long expireTime = 3600;// token过期时间
        long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
        Date expiration = new Date(expireEndTime);
        PolicyConditions policyConds = new PolicyConditions();
        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
        policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, OSSBucketType.getDir(type));

        String postPolicy = client.generatePostPolicy(expiration, policyConds);
        byte[] binaryData = new byte[0];
        binaryData = postPolicy.getBytes("utf-8");

        String encodedPolicy = BinaryUtil.toBase64String(binaryData);
        String postSignature = client.calculatePostSignature(postPolicy);

        respMap.put("accessId", OSSInfo.ACCESS_ID);
        respMap.put("policy", encodedPolicy);
        respMap.put("signature", postSignature);
        //respMap.put("expire", formatISO8601Date(expiration));
        respMap.put("dir", OSSBucketType.getDir(type));
        respMap.put("host", host);
        respMap.put("expire", String.valueOf(expireEndTime / 1000));

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }



    return respMap;
}
 
开发者ID:TZClub,项目名称:OMIPlatform,代码行数:40,代码来源:ALiOSSTokenServer.java

示例4: putObject

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static void putObject(String bucketName, File file, String fileName)
		throws IOException {

	// 初始化OSSClient
	OSSClient client = getOSSClient();

	// 获取指定文件的输入流
	// File file = new File(fileRealPath + fileName);
	InputStream content = new FileInputStream(file);

	// 创建上传Object的Metadata
	ObjectMetadata meta = new ObjectMetadata();

	// 必须设置ContentLength
	// meta.setContentLength(file.length());
	meta.setContentEncoding("UTF-8");
	// 上传Object.
	String key = fileName;
	PutObjectResult result = client.putObject(bucketName, key, content,
			meta);
	content.close();
}
 
开发者ID:wkeyuan,项目名称:DWSurvey,代码行数:23,代码来源:AliyunOSS.java

示例5: AliyunossProvider

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public AliyunossProvider(String urlprefix,String endpoint, String bucketName, String accessKey, String secretKey,boolean isPrivate) {
	
	Validate.notBlank(endpoint, "[endpoint] not defined");
	Validate.notBlank(bucketName, "[bucketName] not defined");
	Validate.notBlank(accessKey, "[accessKey] not defined");
	Validate.notBlank(secretKey, "[secretKey] not defined");
	Validate.notBlank(urlprefix, "[urlprefix] not defined");
	
	ossClient = new OSSClient(endpoint, accessKey, secretKey);
	this.bucketName = bucketName;
	this.urlprefix = urlprefix.endsWith("/") ? urlprefix : (urlprefix + "/");
	this.isPrivate = isPrivate;
	if (!ossClient.doesBucketExist(bucketName)) {
		System.out.println("Creating bucket " + bucketName + "\n");
           ossClient.createBucket(bucketName);
           CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
           createBucketRequest.setCannedACL(isPrivate ? CannedAccessControlList.Private : CannedAccessControlList.PublicRead);
           ossClient.createBucket(createBucketRequest);
	}
}
 
开发者ID:vakinge,项目名称:jeesuite-libs,代码行数:21,代码来源:AliyunossProvider.java

示例6: main

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
		
	    // endpoint以杭州为例,其它region请按实际情况填写
	    String endpoint = "http://oss-cn-beijing.aliyuncs.com";
	    // accessKey请登录https://ak-console.aliyun.com/#/查看
	    String accessKeyId = "aJz5xtsPpE9YeL8N";
	    String accessKeySecret = "qcoAmZWWNpraprwt9S6KbAGXiYploW";
	    ClientConfiguration conf = new ClientConfiguration();
	    // 设置OSSClient使用的最大连接数,默认1024
	    conf.setMaxConnections(200);
	    // 设置请求超时时间,默认50秒
	    conf.setSocketTimeout(10000);
	    // 设置失败请求重试次数,默认3次
	    conf.setMaxErrorRetry(5);
	    
	    // 创建OSSClient实例
	    OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
	    client.deleteObject("sourcod", "aa/cccccc.jpg");
	    // 使用访问OSS
//	    String content = "Hello OSS";
//	    PutObjectResult result =  client.putObject("sourcod", "aa/cccccc.jpg", new URL("http://img2.hitumedia.com/validateImage/20161115/201611151602400098434.jpg").openStream());
//	    InputStream a = result.getCallbackResponseBody();
//	    System.out.println(result.getETag());
	    // 关闭client
	    client.shutdown();
	}
 
开发者ID:WilleamZhao,项目名称:sourcod,代码行数:27,代码来源:Test_aliyunoss.java

示例7: test

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
@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();
}
 
开发者ID:dzh,项目名称:jframe,代码行数:23,代码来源:TestOSSService.java

示例8: doesBucketExist

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
/**
 * 判断Bucket是否存在<br>.
 *
 * @param client     client
 * @param bucketName bucketName
 * @return boolean
 */
public static boolean doesBucketExist(OSSClient client, String bucketName) {
	// 获取Bucket的存在信息
	boolean exists = client.doesBucketExist(bucketName);
	// 输出结果
	if (exists) {
		logger.debug("Bucket: " + bucketName + " is exists");
	} else {
		logger.debug("Bucket: " + bucketName + " is not exists");
	}
	return exists;
}
 
开发者ID:funtl,项目名称:framework,代码行数:19,代码来源:BucketService.java

示例9: newOSSClient

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
private static OSSClient newOSSClient() {
    // 创建ClientConfiguration实例,按照您的需要修改默认参数
    ClientConfiguration conf = new ClientConfiguration();
    // 开启支持CNAME选项
    conf.setSupportCname(supportCname);
    return new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
}
 
开发者ID:tech-microworld,项目名称:maven-cdn-alioss-plugin,代码行数:8,代码来源:AliyunOssUtills.java

示例10: createBucket

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static void createBucket(String bucketName) {
    OSSClient ossClient = newOSSClient();
    try {
        ossClient.createBucket(bucketName);
    } finally {
        ossClient.shutdown();
    }
}
 
开发者ID:tech-microworld,项目名称:maven-cdn-alioss-plugin,代码行数:9,代码来源:AliyunOssUtills.java

示例11: listBuckets

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static List<Bucket> listBuckets() {
    OSSClient ossClient = newOSSClient();
    try {
        return ossClient.listBuckets();
    } finally {
        ossClient.shutdown();
    }
}
 
开发者ID:tech-microworld,项目名称:maven-cdn-alioss-plugin,代码行数:9,代码来源:AliyunOssUtills.java

示例12: putObject

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
/**
 * 上传文件
 *
 * @param path 文件路径
 * @param file 文件对象
 */
public static void putObject(String path, File file) {
    OSSClient ossClient = newOSSClient();
    try {
        ossClient.putObject(bucketName, path, file);
    } finally {
        ossClient.shutdown();
    }
}
 
开发者ID:tech-microworld,项目名称:maven-cdn-alioss-plugin,代码行数:15,代码来源:AliyunOssUtills.java

示例13: listObjects

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static ObjectListing listObjects(ListObjectsRequest listObjectsRequest) {
    OSSClient ossClient = newOSSClient();
    try {
        return ossClient.listObjects(listObjectsRequest);
    } finally {
        ossClient.shutdown();
    }
}
 
开发者ID:tech-microworld,项目名称:maven-cdn-alioss-plugin,代码行数:9,代码来源:AliyunOssUtills.java

示例14: FileUtil

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public FileUtil(@Value("${eagle-oj.oss.end-point}") String endPoint,
                @Value("${eagle-oj.oss.access-key}") String key,
                @Value("${eagle-oj.oss.access-secret}") String secret,
                @Value("${eagle-oj.oss.bucket}") String bucket) {
    ossClient = new OSSClient(endPoint, key, secret);
    BUCKET = bucket;
}
 
开发者ID:Eagle-OJ,项目名称:eagle-oj-api,代码行数:8,代码来源:FileUtil.java

示例15: createBucket

import com.aliyun.oss.OSSClient; //导入依赖的package包/类
/**
 * 创建bucket,阿里设定最多10个bucketName
 * @param bucketName bucket名称
 * @param isPublic 是否可以公开访问
 */
public void createBucket(OSSClient ossClient, String bucketName, Boolean isPublic){
	boolean hasExists = ossClient.doesBucketExist(bucketName);
	if (!hasExists) {
		// 新建一个Bucket
		ossClient.createBucket(bucketName);
		// 设置操作权限对外公开
		ossClient.setBucketAcl(bucketName, isPublic? CannedAccessControlList.PublicRead: CannedAccessControlList.Private);
	}
}
 
开发者ID:lemon-china,项目名称:lemon-fileservice,代码行数:15,代码来源:FileUploadService.java


注:本文中的com.aliyun.oss.OSSClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。