com.aliyun.oss.model.CannedAccessControlList Java Examples

The following examples show how to use com.aliyun.oss.model.CannedAccessControlList. 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: OssClientWrapper.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public Optional<Bucket> getBucket(String bucketName, boolean createIfNotExist) {
		Bucket bucket = null;
		if (!ossClient.doesBucketExist(bucketName)) {
			if(!createIfNotExist){
				return Optional.empty();
			}
            /*
             * Create a new OSS bucket
             */
			if(logger.isInfoEnabled()){
				logger.info("Creating bucket {}", bucketName);
			}
//            ossClient.createBucket(bucketName);
            CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            bucket = ossClient.createBucket(createBucketRequest);
        }else{
        	bucket = ossClient.getBucketInfo(bucketName).getBucket();
        }
		return Optional.ofNullable(bucket);
	}
 
Example #2
Source File: AliyunossProvider.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
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");
	
	this.accessKeyId = accessKey;
	ossClient = new OSSClient(endpoint, accessKey, secretKey);
	this.bucketName = bucketName;
	this.urlprefix = urlprefix.endsWith("/") ? urlprefix : (urlprefix + "/");
	this.isPrivate = isPrivate;
	this.host = StringUtils.remove(urlprefix,"/").split(":")[1];
	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);
	}
}
 
Example #3
Source File: OssBootUtil.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 上传文件到oss
 * @param stream
 * @param relativePath
 * @return
 */
public static String upload(InputStream stream, String relativePath) {
    String FILE_URL = null;
    String fileUrl = relativePath;
    initOSS(endPoint, accessKeyId, accessKeySecret);
    if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
        FILE_URL = staticDomain + "/" + relativePath;
    } else {
        FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
    }
    PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(),stream);
    // 设置权限(公开读)
    ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    if (result != null) {
        log.info("------OSS文件上传成功------" + fileUrl);
    }
    return FILE_URL;
}
 
Example #4
Source File: OssBootUtil.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * 上传文件至阿里云 OSS
 * 文件上传成功,返回文件完整访问路径
 * 文件上传失败,返回 null
 *
 * @param file    待上传文件
 * @param fileDir 文件保存目录
 * @return oss 中的相对文件路径
 */
public static String upload(MultipartFile file, String fileDir) {
    initOSS(endPoint, accessKeyId, accessKeySecret);
    StringBuilder fileUrl = new StringBuilder();
    try {
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        if (!fileDir.endsWith("/")) {
            fileDir = fileDir.concat("/");
        }
        fileUrl = fileUrl.append(fileDir + fileName);

        if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
            FILE_URL = staticDomain + "/" + fileUrl;
        } else {
            FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
        }
        PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(), file.getInputStream());
        // 设置权限(公开读)
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        if (result != null) {
            System.out.println("------OSS文件上传成功------" + fileUrl);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return FILE_URL;
}
 
Example #5
Source File: OssBootUtil.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 上传文件到oss
 * @param stream
 * @param relativePath
 * @return
 */
public static String upload(InputStream stream, String relativePath) {
    String FILE_URL = null;
    String fileUrl = relativePath;
    initOSS(endPoint, accessKeyId, accessKeySecret);
    if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
        FILE_URL = staticDomain + "/" + relativePath;
    } else {
        FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
    }
    PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(),stream);
    // 设置权限(公开读)
    ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    if (result != null) {
        log.info("------OSS文件上传成功------" + fileUrl);
    }
    return FILE_URL;
}
 
Example #6
Source File: OssObjectMetadata.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * Convert to {@link com.aliyun.oss.model.ObjectMetadata}
 * 
 * @return
 */
public com.aliyun.oss.model.ObjectMetadata toAliyunOssObjectMetadata() {
	com.aliyun.oss.model.ObjectMetadata _metadata = new com.aliyun.oss.model.ObjectMetadata();
	_metadata.setUserMetadata(getUserMetadata());
	_metadata.setContentLength(getContentLength());
	_metadata.setContentType(getContentType());
	_metadata.setContentMD5(getContentMd5());
	_metadata.setContentEncoding(getContentEncoding());
	_metadata.setCacheControl(getCacheControl());
	_metadata.setContentDisposition(getContentDisposition());
	_metadata.setLastModified(new Date(getMtime()));
	_metadata.setExpirationTime(new Date(getEtime()));
	_metadata.setObjectAcl(CannedAccessControlList.parse(getAcl().toString()));

	_metadata.setServerSideEncryptionKeyId(getServerSideEncryptionKeyId());
	setServerSideEncryption(getServerSideEncryption());
	return _metadata;
}
 
Example #7
Source File: OSSBootUtil.java    From jeewx-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件至阿里云 OSS
 * 文件上传成功,返回文件完整访问路径
 * 文件上传失败,返回 null
 *
 * @param file    待上传文件
 * @param fileDir 文件保存目录
 * @return oss 中的相对文件路径
 */
public static String upload(FileItemStream file, String fileDir) {
    initOSS(endPoint, accessKeyId, accessKeySecret);
    StringBuilder fileUrl = new StringBuilder();
    try {
        String suffix = file.getName().substring(file.getName().lastIndexOf('.'));
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        if (!fileDir.endsWith("/")) {
            fileDir = fileDir.concat("/");
        }
        fileUrl = fileUrl.append(fileDir + fileName);

        if (oConvertUtils.isNotEmpty(imgDomain)) {
            FILE_URL = imgDomain + "/"  + fileUrl;
        } else {
            FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
        }

        PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(), file.openStream());
        // 设置权限(公开读)
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        if (result != null) {
            System.out.println("------OSS文件上传成功------" + fileUrl);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return FILE_URL;
}
 
Example #8
Source File: OssBootUtil.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件至阿里云 OSS
 * 文件上传成功,返回文件完整访问路径
 * 文件上传失败,返回 null
 *
 * @param file    待上传文件
 * @param fileDir 文件保存目录
 * @return oss 中的相对文件路径
 */
public static String upload(FileItemStream file, String fileDir) {
    String FILE_URL = null;
    initOSS(endPoint, accessKeyId, accessKeySecret);
    StringBuilder fileUrl = new StringBuilder();
    try {
        String suffix = file.getName().substring(file.getName().lastIndexOf('.'));
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        if (!fileDir.endsWith("/")) {
            fileDir = fileDir.concat("/");
        }
        fileUrl = fileUrl.append(fileDir + fileName);

        if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
            FILE_URL = staticDomain + "/" + fileUrl;
        } else {
            FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
        }
        PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(), file.openStream());
        // 设置权限(公开读)
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        if (result != null) {
            log.info("------OSS文件上传成功------" + fileUrl);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return FILE_URL;
}
 
Example #9
Source File: AliyunUtil.java    From roncoo-education with MIT License 5 votes vote down vote up
/**
 * 文件存储入OSS
 * 
 * @param bucketName
 * @param key
 * @param inputStream
 */
private static PutObjectResult putObjectForFile(String endpoint, String keyId, String keySecret, String bucketName, String key, InputStream inputStream, String fileName) {
	OSSClient ossClient = getOssClient(endpoint, keyId, keySecret);
	ObjectMetadata meta = new ObjectMetadata();
	if (StringUtils.isNotBlank(fileName)) {
		meta.setContentDisposition("attachment;filename={}".replace("{}", fileName));
		meta.setObjectAcl(CannedAccessControlList.Private);
	}
	return ossClient.putObject(bucketName, key, inputStream, meta);
}
 
Example #10
Source File: OssBootUtil.java    From teaching with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件至阿里云 OSS
 * 文件上传成功,返回文件完整访问路径
 * 文件上传失败,返回 null
 *
 * @param file    待上传文件
 * @param fileDir 文件保存目录
 * @return oss 中的相对文件路径
 */
public static String upload(FileItemStream file, String fileDir) {
    initOSS(endPoint, accessKeyId, accessKeySecret);
    StringBuilder fileUrl = new StringBuilder();
    try {
        String suffix = file.getName().substring(file.getName().lastIndexOf('.'));
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        if (!fileDir.endsWith("/")) {
            fileDir = fileDir.concat("/");
        }
        fileUrl = fileUrl.append(fileDir + fileName);

        if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
            FILE_URL = staticDomain + "/" + fileUrl;
        } else {
            FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
        }
        PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(), file.openStream());
        // 设置权限(公开读)
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        if (result != null) {
            System.out.println("------OSS文件上传成功------" + fileUrl);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return FILE_URL;
}
 
Example #11
Source File: OssBootUtil.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件至阿里云 OSS
 * 文件上传成功,返回文件完整访问路径
 * 文件上传失败,返回 null
 *
 * @param file    待上传文件
 * @param fileDir 文件保存目录
 * @return oss 中的相对文件路径
 */
public static String upload(FileItemStream file, String fileDir) {
    String FILE_URL = null;
    initOSS(endPoint, accessKeyId, accessKeySecret);
    StringBuilder fileUrl = new StringBuilder();
    try {
        String suffix = file.getName().substring(file.getName().lastIndexOf('.'));
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        if (!fileDir.endsWith("/")) {
            fileDir = fileDir.concat("/");
        }
        fileUrl = fileUrl.append(fileDir + fileName);

        if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
            FILE_URL = staticDomain + "/" + fileUrl;
        } else {
            FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
        }
        PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(), file.openStream());
        // 设置权限(公开读)
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        if (result != null) {
            log.info("------OSS文件上传成功------" + fileUrl);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return FILE_URL;
}
 
Example #12
Source File: OSSBootUtil.java    From jeewx-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件至阿里云 OSS
 * 文件上传成功,返回文件完整访问路径
 * 文件上传失败,返回 null
 *
 * @param file    待上传文件
 * @param fileDir 文件保存目录
 * @return oss 中的相对文件路径
 */
public static String upload(MultipartFile file, String fileDir) {
    initOSS(endPoint, accessKeyId, accessKeySecret);
    StringBuilder fileUrl = new StringBuilder();
    try {
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        if (!fileDir.endsWith("/")) {
            fileDir = fileDir.concat("/");
        }
        fileUrl = fileUrl.append(fileDir + fileName);

        if (oConvertUtils.isNotEmpty(imgDomain)) {
            FILE_URL = imgDomain + "/" + fileUrl;
        } else {
            FILE_URL = "https://" + bucketName + "." + endPoint + "/" + fileUrl;
        }

        PutObjectResult result = ossClient.putObject(bucketName, fileUrl.toString(), file.getInputStream());
        // 设置权限(公开读)
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        if (result != null) {
            System.out.println("------OSS文件上传成功------" + fileUrl);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return FILE_URL;
}
 
Example #13
Source File: BucketEntity.java    From OneBlog with GNU General Public License v3.0 4 votes vote down vote up
public BucketEntity setAcl(CannedAccessControlList acl) {
    this.acl = acl;
    return this;
}
 
Example #14
Source File: InstallController_.java    From wangmarket with Apache License 2.0 4 votes vote down vote up
/**
 * 第三步,验证AccessKey的id、screct的有效性,并初始化创建OSS
 */
@RequestMapping("/accessKeySave${url.suffix}")
@ResponseBody
public BaseVO accessKeySave(
		@RequestParam(value = "id", required = false, defaultValue="") String id,
		@RequestParam(value = "secret", required = false, defaultValue="") String secret
		){
	if(!Global.get("IW_AUTO_INSTALL_USE").equals("true")){
		return error("系统已禁止使用此!");
	}
	if(id.length() == 0){
		return error("请输入 Access Key ID");
	}
	if(secret.length() == 0){
		return error("请输入 Access Key Secret");
	}
	
	//进行自动创建OSS测试
	String bucketName = Global.get("ALIYUN_OSS_BUCKETNAME");
	if(bucketName == null){
		return error("数据表system中没有ALIYUN_OSS_BUCKETNAME,数据表有缺,初始化OSS失败!");
	}else{
		if(bucketName.equals("auto")){
			//若是为auto,则是第一次刚开始用,自动创建
			bucketName = "wangmarket"+DateUtil.timeForUnix10();
		}
	}
	
	OSSUtil.accessKeyId = id;
	OSSUtil.accessKeySecret = secret;
	OSSUtil.bucketName = bucketName;
	OSSUtil.endpoint = ConfigManagerUtil.getSingleton("xnx3Config.xml").getValue("aliyunOSS.endpoint");
	OSSUtil.refreshUrl();
	
	try {
		//判断这个bucketName是否存在
		boolean have = OSSUtil.getOSSClient().doesBucketExist(bucketName);
		if(!have){
			//如果不存在这个bucketName,则自动创建一个
			CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
			createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);	// 设置bucket权限为公共读,默认是私有读写
			OSSUtil.getOSSClient().createBucket(createBucketRequest);
			System.out.println("自动创建buckname: "+bucketName);
		}else{
			System.out.println("OSS未自动创建!因为检测到BucketName已存在!若不是您手动创建的,则建议您按照以下两点进行操作,然后再来创建。");
			System.out.println("1.将/src/xnx3Config.xml文件中,aliyunOSS节点下的bucketName设置为空,即将其中配置的数据删除掉");
			System.out.println("2.将数据表system中,name为ALIYUN_OSS_BUCKETNAME的这一列,将其值改为auto");
			return error("OSS未自动创建!因为检测到BucketName已存在!若不是您手动创建的,则建议您按照以下两点进行操作,然后再来创建。");
		}
	} catch (Exception e) {
		return error("操作失败!错误代码:"+e.getMessage().toString());
	}
	
	//将其存入system数据表
	sqlService.executeSql("update system set value = '"+id+"' WHERE name = 'ALIYUN_ACCESSKEYID'");
	sqlService.executeSql("update system set value = '"+secret+"' WHERE name = 'ALIYUN_ACCESSKEYSECRET'");
	sqlService.executeSql("update system set value = '"+bucketName+"' WHERE name = 'ALIYUN_OSS_BUCKETNAME'");
	
	//设置为禁止安装
	sqlService.executeSql("update system set value = 'false' WHERE name = 'IW_AUTO_INSTALL_USE'");
	
	//更新缓存
	systemService.refreshSystemCache();
	
	return success();
}
 
Example #15
Source File: OssClientWrapper.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public ObjectOperation accessPrivate(){
	access(CannedAccessControlList.Private);
	return this;
}
 
Example #16
Source File: OssClientWrapper.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public ObjectOperation accessPublicRead(){
	access(CannedAccessControlList.PublicRead);
	return this;
}
 
Example #17
Source File: BucketService.java    From cms with Apache License 2.0 2 votes vote down vote up
/**
 *  Bucket权限控制 
 * 
 * public-read-write:
 * 任何人(包括匿名访问)都可以对该bucket中的object进行上传、下载和删除操作;所有这些操作产生的费用由该bucket的创建者承担
 * ,请慎用该权限。<br>
 * public-read: 只有该bucket的创建者可以对该bucket内的Object进行写操作(包括上传和删除);任何人(包括匿名访问)
 * 可以对该bucket中的object进行读操作。<br>
 * private: 只有该bucket的创建者才可以访问此Bukcet。其他人禁止对此Bucket做任何操作。<br>
 * @param bucketName
 * @param acl CannedAccessControlList是枚举类型,包含三个值: Private 、 PublicRead 、 PublicReadWrite ,它们分别对应相关权限
 */
void setBucketAcl( String bucketName, CannedAccessControlList acl);