本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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();
}
示例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);
}
}
示例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();
}
示例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();
}
示例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;
}
示例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);
}
示例10: createBucket
import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static void createBucket(String bucketName) {
OSSClient ossClient = newOSSClient();
try {
ossClient.createBucket(bucketName);
} finally {
ossClient.shutdown();
}
}
示例11: listBuckets
import com.aliyun.oss.OSSClient; //导入依赖的package包/类
public static List<Bucket> listBuckets() {
OSSClient ossClient = newOSSClient();
try {
return ossClient.listBuckets();
} finally {
ossClient.shutdown();
}
}
示例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();
}
}
示例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();
}
}
示例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;
}
示例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);
}
}