com.aliyun.oss.model.CreateBucketRequest Java Examples

The following examples show how to use com.aliyun.oss.model.CreateBucketRequest. 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: 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 #2
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 #3
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();
}