Bucket是OSS上的命名空间,权限控制、日志记录等高级功能的管理实体;Bucket名称在整个OSS服务中具有全局唯一性,且不能修改;存储在OSS上的每个Object必须都包含在某个Bucket中。一个应用,例如图片分享网站,可以对应一个或多个Bucket。一个用户最多可创建10个Bucket,但每个Bucket中存放的Object的数量和大小总和没有限制,用户不需要考虑数据的可扩展性。

    Bucket的创建,删除,访问代码如下:

/**
* 阿里对象存储OSS数据管理
 * */
public class AliOSSClientEx extends OSSClient {
 private static final Log log = LogFactory.getLog(AliOSSClientEx.class);


 private ServiceClient serviceClientEx;
 private LiveChannelOperationEx liveChannelOperationEx;
 private String endPoint;
 private String bucketName;


 /**
  * Create one OSSClient object
  * */
 public static AliOSSClientEx getOSSClient(String bucketName, String endPoint, String accessKeyId,
 String accessKeySecret) {
 try {
 ClientConfiguration conf = new ClientConfiguration();
 // 设置OSSClient使用的最大连接数,默认1024
 conf.setMaxConnections(512);
 // 设置请求超时时间,默认50秒
 conf.setSocketTimeout(60 * 1000);
 // 设置失败请求重试次数,默认3次
 conf.setMaxErrorRetry(3);
 DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
 AliOSSClientEx ossClient = new AliOSSClientEx(bucketName, endPoint, credentialProvider, conf);
 return ossClient;
 } catch (Throwable t) {
 log.error("Create Ali OSSClient exception," + endPoint, t);
 return null;
 }
 }


 public AliOSSClientEx(String bucketName, String endPoint, DefaultCredentialProvider credentialProvider,
 ClientConfiguration conf) {
 super(endPoint, credentialProvider, conf);
 this.bucketName = bucketName;
 this.endPoint = endPoint;
 this.initOperations(conf, credentialProvider);
 }


 /**
  * @param bucketName
  * */
 public String createOSSBucket(String bucketName) throws Exception {
 try {
 Bucket bucket = null;
 if (!super.doesBucketExist(bucketName)) {
 bucket = super.createBucket(new CreateBucketRequest(bucketName));
 return bucket.getName() + "." + endPoint;
 } else {
 BucketInfo bucketInfo = getBucketInfo(bucketName);
 return bucketInfo.getBucket().getName() + "." + endPoint;
 }
 } catch (Exception ex) {
 log.error("Create bucket exception:", ex);
 throw ex;
 }
 }


 /**
  * @param bucketName
  * */
 public boolean deleteOSSBucket(String bucketName) {
 try {
 if (super.doesBucketExist(bucketName)) {
 super.deleteBucket(bucketName);
 return true;
 }
 } catch (Exception ex) {
 log.error("Delete bucket exception", ex);
 }
 return false;
 }


 public boolean isBucketExists(String bucketName) {
 List<Bucket> buckets = listBuckets();
 if (buckets != null) {
 for (Bucket bucket : buckets) {
 if (bucketName.equals(bucket.getName())) {
 return true;
 }
 }
 }
 return false;
 }


 /**
  * @param liveChannelName
  * 
  * @return 返回推流地址
  * */
 public String createLiveChannel(String liveChannelName) throws Exception {
 try {
 CreateLiveChannelRequest liveChannelRequest = new CreateLiveChannelRequest(bucketName, liveChannelName);
 CreateLiveChannelResult liveChannelResult = super.createLiveChannel(liveChannelRequest);
 List<String> urls = liveChannelResult.getPublishUrls();
 if (urls != null && urls.size() > 0) {
 return urls.get(0);
 }
 return null;
 } catch (Exception ex) {
 log.error("Create default live channel exception,liveChannel=" + liveChannelName, ex);
 throw ex;
 }
 }


 /**
  * @param liveChannelName
  * @return 返回指定LiveChannel最近的10次推流记录
  * */
 public List<LiveRecord> getLiveChannelHistory(String liveChannelName) {
 try {
 List<LiveRecord> liveRecords = super.getLiveChannelHistory(bucketName, liveChannelName);
 return liveRecords;
 } catch (Exception ex) {
 log.error("Get live channel history record exception,liveChannel=" + liveChannelName, ex);
 return null;
 }
 }


 /**
  * 
  * @param liveChannelName
  * @return 获取指定LiveChannel的推流状态信息。
  * 
  * */
 public LiveChannelStat getLiveChannelStat(String liveChannelName) {
 try {
 LiveChannelStat stat = super.getLiveChannelStat(bucketName, liveChannelName);
 return stat;
 } catch (Exception ex) {
 log.error("Get live channel state exception,liveChannel=" + liveChannelName, ex);
 return null;
 }
 }


 /**
  * @param liveChannelName
  * @return 获取指定LiveChannel的配置信息
  * */
 public LiveChannelInfo getLiveChannelInfo(String liveChannelName) {
 try {
 LiveChannelInfo channelInfo = super.getLiveChannelInfo(bucketName, liveChannelName);
 return channelInfo;
 } catch (Exception ex) {
 log.error("Get live channel info exception,liveChannel=" + liveChannelName, ex);
 return null;
 }
 }


 /**
  * 获取Backet下的所有liveChannels
  * 
  * @return
  * */
 public List<LiveChannel> getLiveChannels() {
 try {
 List<LiveChannel> channels = super.listLiveChannels(bucketName);
 return channels;
 } catch (Exception ex) {
 log.error("Get bucket live channels exception.", ex);
 return null;
 }
 }


 /**
  * 设置LiveChanel存储对象的生命周期,Lifecycle开启后,OSS将按照配置,
  * 定期自动删除或转储与Lifecycle规则相匹配的Object。
  * 
  * @param liveChannelName
  *            :
  * 
  * @param days
  *            :指定规则在对象最后修改时间过后多少天生效。
  * 
  * */
 public boolean setChannelLifecycle(String channelId, String liveChannelName, int days) {
 try {
 SetBucketLifecycleRequest lifecycleRequest = new SetBucketLifecycleRequest(bucketName);
 LifecycleRule lifecycleRule = new LifecycleRule();
 lifecycleRule.setId(channelId);
 lifecycleRule.setExpirationDays(days);
 lifecycleRule.setPrefix(liveChannelName + "/");
 lifecycleRule.setStatus(RuleStatus.Enabled);
 lifecycleRequest.AddLifecycleRule(lifecycleRule);
 this.setBucketLifecycle(lifecycleRequest);
 return true;
 } catch (Exception ex) {
 log.error("Set live channel object life cycle exception,liveChannel=" + liveChannelName, ex);
 return false;
 }
 }


 /**
  * 删除LiveChanel存储对象的生命周期,实际上并不是真正的删除,而是把生命周期规则设备为无效
  * 
  * @param liveChannelName
  *            :
  * 
  * */
 public boolean deleteChannelLifecycle(String channelId, String liveChannelName) {
 try {
 SetBucketLifecycleRequest lifecycleRequest = new SetBucketLifecycleRequest(bucketName);
 LifecycleRule lifecycleRule = new LifecycleRule();
 lifecycleRule.setId(channelId);
 lifecycleRule.setExpirationDays(99999);
 lifecycleRule.setPrefix(liveChannelName + "/");
 lifecycleRule.setStatus(RuleStatus.Disabled);
 lifecycleRequest.AddLifecycleRule(lifecycleRule);
 this.setBucketLifecycle(lifecycleRequest);
 return true;
 } catch (Exception ex) {
 log.error("Set live channel object life cycle exception,liveChannel=" + liveChannelName, ex);
 return false;
 }
 }


 /**
  * 用来为指定LiveChannel推流生成的,指定时间段内的ts文件生成一个点播用的播放列表(m3u8文件)
  * 
  * @param liveChannelName
  * @param playListName
  * @param startTime
  *            :开始时间 Unix timestamp(秒)
  * @param entTime
  *            :结束时间 Unix timestamp(秒) endTime必须大于startTime,且时间跨度不能大于1天。
  * 
  * @return OSS会查询指定时间范围内的所有该LiveChannel推流生成的ts文件,并将其拼装为一个播放列表
  * @throws Exception
  * */
 public VodResult postVodPlaylist(String liveChannelName, String playListName, long startTime, long endTime)
 throws Exception {
 if (startTime >= endTime) {
 throw new Exception("Start time must be less than the end time.");
 }
 if ((endTime - startTime) >= 24 * 3600) {
 throw new Exception("The time span of the start and end times cannot be greater than one day.");
 }


 try {
 GenerateVodPlaylistRequest generateVodPlaylistRequest = new GenerateVodPlaylistRequest(bucketName,
 liveChannelName, playListName, startTime, endTime);
 return liveChannelOperationEx.generateVodPlaylistEx(generateVodPlaylistRequest);
 } catch (Exception ex) {
 log.error("Get vod play list exception,liveName=" + liveChannelName + ",playListName=" + playListName, ex);
 return null;
 }
 }


 /**
  * 获取指定的LiveChannel下的所有ts文件
  * 
  * @param liveChannel
  *            :
  * @param prefix
  *            :限定返回的object key必须以prefix作为前缀
  * @return List<ChannelFileSummary>
  * */
 public List<ChannelFileSummary> getLiveChannelFiles(String liveChannel, String prefix) {
 return getLiveChannelFiles(liveChannel, prefix, null);
 }


 /**
  * 获取指定的LiveChannel下的所有ts文件
  * 
  * @param liveChannel
  *            :
  * @param prefix
  *            : 限定返回的object key必须以prefix作为前缀。
  * @param marker
  *            :设定结果从marker之后按字母排序的第一个开始返回
  * 
  * */
 public List<ChannelFileSummary> getLiveChannelFiles(String liveChannel, String prefix, String marker) {
 try {
 final int maxKeys = 999;
 List<ChannelFileSummary> fileSummaries = new ArrayList<ChannelFileSummary>();
 ListObjectsRequest objectsRequest = new ListObjectsRequest(bucketName);
 objectsRequest.setMaxKeys(maxKeys);
 objectsRequest.setPrefix(liveChannel + "/" + prefix);
 objectsRequest.setMarker(marker);
 ObjectListing objectListing = super.listObjects(objectsRequest);
 if (objectListing.getObjectSummaries().size() > 0) {
 ChannelFileSummary fileSummary = null;
 for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
 fileSummary = new ChannelFileSummary(objectSummary);
 if (liveChannel.equals(fileSummary.getChannelName())) {
 fileSummaries.add(fileSummary);
 }
 }
 }
 return fileSummaries;
 } catch (Exception ex) {
 log.error("Get live channel files exception,liveChannel=" + liveChannel, ex);
 return null;
 }
 }


 /**
  * 查询小时内的所有.ts文件
  * 
  * @param liveChannel
  *            :
  * @param dateHour
  *            :
  * @return List<ChannelFileSummary>
  * */
 public List<ChannelFileSummary> getLiveChannelFilesByTime(String liveChannel, Date dateHour) {
 Calendar beginCalendar = Calendar.getInstance();
 beginCalendar.setTime(dateHour);
 beginCalendar.set(Calendar.MINUTE, 0);
 beginCalendar.set(Calendar.SECOND, 0);
 beginCalendar.add(Calendar.MILLISECOND, 0);


 Calendar endCalendar = Calendar.getInstance();
 endCalendar.setTime(dateHour);
 endCalendar.set(Calendar.MINUTE, 59);
 endCalendar.set(Calendar.SECOND, 59);
 endCalendar.add(Calendar.MILLISECOND, 999);
 return getLiveChannelFilesByTime(liveChannel, beginCalendar.getTime(), endCalendar.getTime());
 }


 /**
  * 查询指定的时间范围内的.ts文件
  * 
  * @param liveChannel
  *            :
  * @param beginTime
  *            :
  * @param endTime
  *            :
  * @return List<ChannelFileSummary>
  * */
 public List<ChannelFileSummary> getLiveChannelFilesByTime(String liveChannel, Date beginTime, Date endTime) {
 List<String> lstTimeRange = getTimeRange(beginTime, endTime);
 long startTimestamp = beginTime.getTime();
 long endTimestamp = endTime.getTime();
 List<ChannelFileSummary> channelFileSummaries = new ArrayList<ChannelFileSummary>();
 List<ChannelFileSummary> tempFileSummaries = null;
 for (String timePrefix : lstTimeRange) {
 tempFileSummaries = getLiveChannelFiles(liveChannel, timePrefix);
 if (tempFileSummaries != null) {
 for (ChannelFileSummary fileSummary : tempFileSummaries) {
 if (fileSummary.getFileTimestamp() > 0) {
 if (fileSummary.getFileTimestamp() >= startTimestamp
 && fileSummary.getFileTimestamp() <= endTimestamp) {
 channelFileSummaries.add(fileSummary);
 }
 }
 }
 }
 }


 return channelFileSummaries;
 }


 /**
  * @param liveChannelName
  * 
  * @param playListName
  * 
  * @return 返回推流地址
  * */
 public String createLiveChannel(String liveChannelName, String playListName) {
 try {
 LiveChannelTarget target = new LiveChannelTarget("HLS", 100, 99, playListName);
 CreateLiveChannelRequest createLiveChannelRequest = new CreateLiveChannelRequest(bucketName,
 liveChannelName, "", LiveChannelStatus.Enabled, target);


 CreateLiveChannelResult createLiveChannelResult = super.createLiveChannel(createLiveChannelRequest);
 List<String> urls = createLiveChannelResult.getPublishUrls();
 if (urls != null && urls.size() > 0) {
 return urls.get(0);
 }
 } catch (Exception ex) {
 log.error("Create target live channel exception,liveChannel=" + liveChannelName + ",playListName="
 + playListName, ex);
 }
 return null;
 }


 /**
  * @param liveChannelName
  * @return true:success,false:
  * */
 public boolean deleteLiveChannel(String liveChannelName) {
 try {
 LiveChannelGenericRequest liveChannelRequest = new LiveChannelGenericRequest(bucketName, liveChannelName);
 super.deleteLiveChannel(liveChannelRequest);
 return true;
 } catch (Exception ex) {
 log.error("Delete live channel exception,liveChannel=" + liveChannelName, ex);
 return false;
 }
 }


 /**
  * @param liveChannelName
  *
  * @param playListName
  * @return rtmp地址
  * */
 public String getSignRtmpUrl(String liveChannelName, String playListName) {
 // 设置URL过期时间为1小时
 return getSignRtmpUrl(liveChannelName, playListName, 24 * 3600 * 1000);
 }


 /**
  * @param liveChannelName
  * 
  * @param playListName
  * 
  * @param expires
  *            URL的过期时间,单位:秒
  * @return rtmp地址
  * 
  * */
 public String getSignRtmpUrl(String liveChannelName, String playListName, long expires) {
 try {
 long expiresTime = new Date().getTime() / 1000 + expires;
 GenerateRtmpUriRequest rtmpUriRequest = new GenerateRtmpUriRequest(bucketName, liveChannelName,
 playListName, expiresTime);
 // 生成rtmp签名
 String signedUrl = super.generateRtmpUri(rtmpUriRequest);
 return signedUrl;
 } catch (Exception ex) {
 log.error("Generate rtmp url exception,liveChannel=" + liveChannelName, ex);
 return null;
 }
 }


 private void initOperations(ClientConfiguration config, DefaultCredentialProvider credentialProvider) {
 if (config.isRequestTimeoutEnabled()) {
 this.serviceClientEx = new TimeoutServiceClient(config);
 } else {
 this.serviceClientEx = new DefaultServiceClient(config);
 }
 this.liveChannelOperationEx = new LiveChannelOperationEx(this.serviceClientEx, credentialProvider);
 this.liveChannelOperationEx.setEndpoint(toURI(endPoint));
 }


 private URI toURI(String endpoint) throws IllegalArgumentException {
 if (!endpoint.contains("://")) {
 ClientConfiguration conf = this.serviceClientEx.getClientConfiguration();
 endpoint = conf.getProtocol().toString() + "://" + endpoint;
 }


 try {
 return new URI(endpoint);
 } catch (URISyntaxException e) {
 throw new IllegalArgumentException(e);
 }
 }


 private List<String> getTimeRange(Date beginTime, Date endTime) {
 int begin = (int) (beginTime.getTime() / 1000000);
 int end = (int) (endTime.getTime() / 1000000);
 List<String> lstRange = new ArrayList<String>();
 for (int index = begin; index <= end; index++) {
 lstRange.add(String.valueOf(index));
 }
 return lstRange;
 }}
继承LiveChannelOperation,获取generateVodPlaylist方法结果
public class LiveChannelOperationEx extends LiveChannelOperation {


 private VodResponseParser vodResponseParser;


 public LiveChannelOperationEx(ServiceClient client, CredentialsProvider credsProvider) {
 super(client, credsProvider);
 vodResponseParser = new VodResponseParser();
 }


 public VodResult generateVodPlaylistEx(GenerateVodPlaylistRequest generateVodPlaylistRequest) throws OSSException,
 ClientException {
 assertParameterNotNull(generateVodPlaylistRequest, "generateVodPlaylistRequest");


 String bucketName = generateVodPlaylistRequest.getBucketName();
 String liveChannelName = generateVodPlaylistRequest.getLiveChannelName();
 String playlistName = generateVodPlaylistRequest.getPlaylistName();
 Long startTime = generateVodPlaylistRequest.getStratTime();
 Long endTime = generateVodPlaylistRequest.getEndTime();


 assertParameterNotNull(bucketName, "bucketName");
 ensureBucketNameValid(bucketName);
 assertParameterNotNull(liveChannelName, "liveChannelName");
 ensureLiveChannelNameValid(liveChannelName);
 assertParameterNotNull(playlistName, "playlistName");
 assertParameterNotNull(startTime, "stratTime");
 assertParameterNotNull(endTime, "endTime");


 Map<String, String> parameters = new HashMap<String, String>();
 parameters.put(RequestParameters.SUBRESOURCE_VOD, null);
 parameters.put(RequestParameters.SUBRESOURCE_START_TIME, startTime.toString());
 parameters.put(RequestParameters.SUBRESOURCE_END_TIME, endTime.toString());


 String key = liveChannelName + "/" + playlistName;
 RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
 .setMethod(HttpMethod.POST).setBucket(bucketName).setKey(key).setParameters(parameters)
 .setInputStream(new ByteArrayInputStream(new byte[0])).setInputSize(0)
 .setOriginalRequest(generateVodPlaylistRequest).build();


 return doOperation(request, vodResponseParser, bucketName, key);
 }}