com.aliyun.oss.model.ObjectMetadata Java Examples

The following examples show how to use com.aliyun.oss.model.ObjectMetadata. 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: AliyunStorage.java    From BigDataPlatform with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
    try {
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        objectMetadata.setContentType(contentType);
        // 对象键(Key)是对象在存储桶中的唯一标识。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
        PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }

}
 
Example #2
Source File: AliOSSBlobStoreTest.java    From multiapps-controller with Apache License 2.0 6 votes vote down vote up
@Test
public void testList() throws Exception {
    ObjectListing objectListing = new ObjectListing();
    objectListing.setBucketName(CONTAINER);
    objectListing.setObjectSummaries(getObjectSummaries(3));
    Mockito.when(ossClient.listObjects(any(ListObjectsRequest.class)))
           .thenReturn(objectListing);

    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setUserMetadata(getUserMetadata());

    Mockito.when(ossClient.getObjectMetadata(any(String.class), any(String.class)))
           .thenReturn(objectMetadata);
    Mockito.when(ossClient.generatePresignedUrl(any(), any(), any()))
           .thenReturn(new URL("https://oss-eu-central-1.aliyuncs.com"));
    aliOSSBlobStore.list(CONTAINER, new ListContainerOptions().withDetails())
                   .forEach(storageMetadata -> {
        assertTrue(storageMetadata.getName()
                                  .startsWith(FILENAME));
        assertEquals(PAYLOAD.length(), storageMetadata.getSize());
        assertTrue(storageMetadata.getETag()
                                  .startsWith(FILENAME));
        assertEquals(getUserMetadata(), storageMetadata.getUserMetadata());
    });
}
 
Example #3
Source File: AliyunStorage.java    From litemall with MIT License 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
    try {
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        objectMetadata.setContentType(contentType);
        // 对象键(Key)是对象在存储桶中的唯一标识。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
        PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }

}
 
Example #4
Source File: AliyunStorage.java    From mall with MIT License 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
    try {
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        objectMetadata.setContentType(contentType);
        // 对象键(Key)是对象在存储桶中的唯一标识。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
        PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}
 
Example #5
Source File: ALiYunOSSUploadFileTemplateServiceImpl.java    From plumemo with Apache License 2.0 6 votes vote down vote up
@Override
public String doSaveFileStore(final MultipartFile file) {
    final OSS ossClient = new OSSClientBuilder()
            .build(ConfigCache.getConfig(Constants.ALIYUN_OSS_ENDPOINT),
                    ConfigCache.getConfig(Constants.ALIYUN_OSS_ACCESS_KEY),
                    ConfigCache.getConfig(Constants.ALIYUN_OSS_SECRET_KEY));
    try {
        final String fileName = FileUtil.createSingleFilePath(ConfigCache.getConfig(Constants.ALIYUN_OSS_PATH), file.getOriginalFilename());
        final PutObjectRequest putObjectRequest = new PutObjectRequest(ConfigCache.getConfig(Constants.ALIYUN_OSS_BUCKET), fileName, file.getInputStream());
        final ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        putObjectRequest.setMetadata(metadata);
        ossClient.putObject(putObjectRequest);
        return ConfigCache.getConfig(Constants.ALIYUN_OSS_IMAGE_DOMAIN) + fileName;
    } catch (final IOException e) {
        return "";
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}
 
Example #6
Source File: AliyunOssClient.java    From markdown-image-kit with MIT License 6 votes vote down vote up
/**
 * Upload string.
 *
 * @param ossClient the ossClient client
 * @param instream  the instream
 * @param fileName  the file name
 * @return the string
 */
public String upload(@NotNull OSS ossClient,
                     @NotNull InputStream instream,
                     @NotNull String fileName) {
    try {
        // 创建上传 Object 的 Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(instream.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(ImageUtils.getImageType(fileName));
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        ossClient.putObject(bucketName, filedir + fileName, instream, objectMetadata);
        return getUrl(ossClient, filedir, fileName);
    } catch (IOException | OSSException | ClientException e) {
        log.trace("", e);
    }
    return "";
}
 
Example #7
Source File: AliyunStorage.java    From dts-shop with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
	try {
		logger.info("阿里云存储OSS对象 内容长度:{},文件类型:{},KeyName:{}",contentLength,contentType,keyName);
		// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(contentLength);
		objectMetadata.setContentType(contentType);
		
		// 对象键(Key)是对象在存储桶中的唯一标识。
		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
		
		PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
		if (putObjectResult != null && putObjectResult.getResponse() != null) {
			logger.info("阿里云存储结果code:" + putObjectResult.getResponse().getStatusCode());
		}
	} catch (Exception ex) {
		logger.error("阿里云存储 keyName:{} ,失败:{}",keyName,ex.getMessage());
		ex.printStackTrace();
	}

}
 
Example #8
Source File: AliyunStorageBizServiceImpl.java    From unimall with Apache License 2.0 5 votes vote down vote up
@Override
public String upload(String fileName, InputStream is, long contentLength, String contentType) {
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(contentLength);
    objectMetadata.setContentType(contentType);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, fileName, is, objectMetadata);
    ossClient.putObject(putObjectRequest);
    return baseUrl + fileName;
}
 
Example #9
Source File: USSImageupload.java    From Tbed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 客户端接口
 * */
public Map<ReturnImage, Integer> clientuploadUSS(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception {
    File file = null;
    Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
    //设置Header
    ObjectMetadata meta = new ObjectMetadata();
    meta.setHeader("Content-Disposition", "inline");
    for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
        java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
        String times = format1.format(new Date());
        file = SetFiles.changeFile(entry.getValue());
        // 上传文件流。
        System.out.println("客户端:待上传的图片:"+username + "/" + uuid+times + "." + entry.getKey());
        ReturnImage returnImage = new ReturnImage();
        if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){
            // 例2:采用数据流模式上传文件(节省内存),自动创建父级目录
            upyun.setContentMD5(UpYun.md5(file));
            boolean result = upyun.writeFile(username + "/" + uuid+times + "." + entry.getKey(), file, true);
            if(result){
                returnImage.setImgname(entry.getValue().getOriginalFilename());
                returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
                ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));

                //ImgUrl.put(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey(), (int) (entry.getValue().getSize()));
            }else{
                System.err.println("上传失败");
            }
        }else{
            returnImage.setImgname(entry.getValue().getOriginalFilename());
            returnImage.setImgurl("文件超出系统设定大小,不得超过");
            ImgUrl.put(returnImage, -1);
        }
    }
    return ImgUrl;
}
 
Example #10
Source File: OSSImageupload.java    From Tbed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 客户端接口
 * */
public Map<ReturnImage, Integer> clientuploadOSS(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception {
        File file = null;
        Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
        ObjectMetadata meta = new ObjectMetadata();
        meta.setHeader("Content-Disposition", "inline");
        for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
            java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
            String times = format1.format(new Date());
            file = SetFiles.changeFile(entry.getValue());
            String head = "";
            if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){
                head = "image/jpeg";
            }else if(entry.getKey().equals("png")){
                head = "image/png";
            }else if(entry.getKey().equals("bmp")){
                head = "image/bmp";
            }else if(entry.getKey().equals("gif")){
                head = "image/gif";
            }else{
                //System.err.println("位置格式文件,无法定义header头。");
                head = "image/"+entry.getKey();
            }
            meta.setHeader("Content-Type", head);//image/jpeg
            ReturnImage returnImage = new ReturnImage();
            if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){
                ossClient.putObject(key.getBucketname(), username + "/" + uuid+times + "." + entry.getKey(),file,meta);
                returnImage.setImgname(entry.getValue().getOriginalFilename());
                returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
                ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
            }else{
                returnImage.setImgname(entry.getValue().getOriginalFilename());
                returnImage.setImgurl("文件超出系统设定大小,不得超过");
                ImgUrl.put(returnImage, -1);
            }
        }
        return ImgUrl;

}
 
Example #11
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 #12
Source File: OssDownloadTests.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {

		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

		try {
			DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
			// Sets the local file to download to
			downloadFileRequest.setDownloadFile(downloadFile);
			// Sets the concurrent task thread count 5. By default it's 1.
			downloadFileRequest.setTaskNum(5);
			// Sets the part size, by default it's 100K.
			downloadFileRequest.setPartSize(1024 * 1024 * 1);
			// Enable checkpoint. By default it's false.
			downloadFileRequest.setEnableCheckpoint(true);

			DownloadFileResult downloadResult = ossClient.downloadFile(downloadFileRequest);

			ObjectMetadata objectMetadata = downloadResult.getObjectMetadata();
			System.out.println(objectMetadata.getETag());
			System.out.println(objectMetadata.getLastModified());
			System.out.println(objectMetadata.getUserMetadata().get("meta"));

		} catch (OSSException oe) {
			System.out.println("Caught an OSSException, which means your request made it to OSS, "
					+ "but was rejected with an error response for some reason.");
			System.out.println("Error Message: " + oe.getErrorMessage());
			System.out.println("Error Code:       " + oe.getErrorCode());
			System.out.println("Request ID:      " + oe.getRequestId());
			System.out.println("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			System.out.println("Caught an ClientException, which means the client encountered "
					+ "a serious internal problem while trying to communicate with OSS, "
					+ "such as not being able to access the network.");
			System.out.println("Error Message: " + ce.getMessage());
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			ossClient.shutdown();
		}
	}
 
Example #13
Source File: OssClientWrapper.java    From onetwo with Apache License 2.0 5 votes vote down vote up
/****
 * 
 * @author wayshall
 * @param object
 * @param contentType example: MediaType.APPLICATION_OCTET_STREAM_VALUE
 * @return
 */
public ObjectOperation storeAsJson(Object object, String contentType){
	String json = JsonMapper.DEFAULT_MAPPER.toJson(object);
	StringReader sr = new StringReader(json);
	ObjectMetadata meta = new ObjectMetadata();
	meta.setContentType(contentType);
	return store(new ReaderInputStream(sr), meta);
}
 
Example #14
Source File: OssClientWrapper.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public ObjectOperation store(File file, ObjectMetadata meta, Consumer<PutObjectResult> onCompleted){
	if(!file.exists()){
		throw new BaseException("file is not exists!");
	}
	PutObjectResult result = putObject(new PutObjectRequest(bucketName, key, file, meta));
	if (onCompleted!=null) {
		onCompleted.accept(result);
	} else {
		if (!result.getResponse().isSuccessful()) {
			throw new BaseException("uplaod to oss error: " + result.getResponse().getErrorResponseAsString());
		}
	}
	return this;
}
 
Example #15
Source File: AliossTemplate.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SneakyThrows
public OssFile statFile(String bucketName, String fileName) {
	ObjectMetadata stat = ossClient.getObjectMetadata(getBucketName(bucketName), fileName);
	OssFile ossFile = new OssFile();
	ossFile.setName(fileName);
	ossFile.setLink(fileLink(ossFile.getName()));
	ossFile.setHash(stat.getContentMD5());
	ossFile.setLength(stat.getContentLength());
	ossFile.setPutTime(stat.getLastModified());
	ossFile.setContentType(stat.getContentType());
	return ossFile;
}
 
Example #16
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@Override
public String putBlob(String container, Blob blob) {
    return doOssOperation(oss -> {
        try {
            ObjectMetadata objectMetadata = createObjectMetadataFromBlob(blob);
            PutObjectRequest request = new PutObjectRequest(container, blob.getMetadata()
                                                                           .getProviderId(), blob.getPayload()
                                                                                                 .openStream(), objectMetadata);
            PutObjectResult result = oss.putObject(request);
            return result.getETag();
        } catch (IOException e) {
            throw new SLException(e);
        }
    });
}
 
Example #17
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private StorageMetadata toStorageMetadata(OSS oss, String container, OSSObjectSummary ossObjectSummary) {
    ObjectMetadata metadata = oss.getObjectMetadata(container, ossObjectSummary.getKey());
    URI url = getPresignedUriForObject(oss, ossObjectSummary);
    return new StorageMetadataImpl(StorageType.BLOB, ossObjectSummary.getKey(), ossObjectSummary.getKey(), defaultLocation.get(),
                                   url, ossObjectSummary.getETag(), ossObjectSummary.getLastModified(),
                                   ossObjectSummary.getLastModified(), metadata.getUserMetadata(), ossObjectSummary.getSize(),
                                   Tier.STANDARD);
}
 
Example #18
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private ObjectMetadata createObjectMetadataFromBlob(Blob blob) {
    ObjectMetadata objectMetadata = new ObjectMetadata();
    ContentMetadata blobContentMetadata = blob.getMetadata()
                                              .getContentMetadata();
    if (blobContentMetadata.getCacheControl() != null) {
        objectMetadata.setCacheControl(blobContentMetadata.getCacheControl());
    }
    if (blobContentMetadata.getContentDisposition() != null) {
        objectMetadata.setContentDisposition(blobContentMetadata.getContentDisposition());
    }
    if (blobContentMetadata.getContentEncoding() != null) {
        objectMetadata.setContentEncoding(blobContentMetadata.getContentEncoding());
    }
    if (blobContentMetadata.getContentLength() != null) {
        objectMetadata.setContentLength(blobContentMetadata.getContentLength());
    }
    if (blobContentMetadata.getContentType() != null) {
        objectMetadata.setContentType(blobContentMetadata.getContentType());
    }
    if (blobContentMetadata.getExpires() != null) {
        objectMetadata.setExpirationTime(blobContentMetadata.getExpires());
    }
    Date lastModified = blob.getMetadata()
                            .getLastModified();
    if (lastModified != null) {
        objectMetadata.setLastModified(lastModified);
    }
    if (blob.getAllHeaders() != null) {
        blob.getAllHeaders()
            .asMap()
            .forEach(objectMetadata::setHeader);
    }
    Map<String, String> userMetadata = blob.getMetadata()
                                           .getUserMetadata();
    if (userMetadata != null) {
        objectMetadata.setUserMetadata(userMetadata);
    }
    return objectMetadata;
}
 
Example #19
Source File: OssClientWrapper.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public ObjectOperation store(InputStream inputStream, ObjectMetadata meta){
	Assert.notNull(inputStream, "inputStream can not be null");
	putObject(new PutObjectRequest(bucketName, key, inputStream, meta));
	return this;
}
 
Example #20
Source File: OssClientWrapper.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public String store(String bucketName, File file, ObjectMetadata meta){
	String key = UUID.randomUUID().toString();
	objectOperation(bucketName, key).store(file, meta, null);
	return getUrl(true, bucketName, key);
}
 
Example #21
Source File: OssClientWrapper.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public String storeWithFileName(String bucketName, File file, ObjectMetadata meta){
	String key = FileUtils.getFileName(file.getPath());
	objectOperation(bucketName, key).store(file, meta, null);
	return getUrl(true, bucketName, key);
}
 
Example #22
Source File: OssUploadServiceImpl.java    From mysiteforme with Apache License 2.0 4 votes vote down vote up
@Override
public String upload(MultipartFile file) throws IOException, NoSuchAlgorithmException {
    String fileName =null,realNames = "";
    StringBuffer returnUrl = new StringBuffer(getUploadInfo().getOssBasePath());
    String ossDir = getUploadInfo().getOssDir();
    try {
        fileName = file.getOriginalFilename();
        //上传文件
        StringBuffer realName = new StringBuffer(UUID.randomUUID().toString());
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        realName.append(fileExtension);
        QETag tag = new QETag();
        String hash = tag.calcETag(file);
        Rescource rescource = new Rescource();
        EntityWrapper<RestResponse> wrapper = new EntityWrapper<>();
        wrapper.eq("hash",hash);
        wrapper.eq("source","oss");
        rescource = rescource.selectOne(wrapper);
        if( rescource!= null){
            return rescource.getWebUrl();
        }

        InputStream is = file.getInputStream();

        Long fileSize = file.getSize();
        //创建上传Object的Metadata
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(is.available());
        metadata.setCacheControl("no-cache");
        metadata.setHeader("Pragma", "no-cache");
        metadata.setContentEncoding("utf-8");
        metadata.setContentType(ToolUtil.getContentType(fileName));
        StringBuffer description = new StringBuffer("filename/filesize=");
        description.append(realNames).append("/").append(fileSize).append("Byte.");
        metadata.setContentDisposition(description.toString());

        StringBuffer key = new StringBuffer();
        if(ossDir != null && !"".equals(ossDir)){
            key.append(ossDir).append("/");
            returnUrl.append(ossDir).append("/");
        }
        key.append(realName);
        returnUrl.append(realName);
        PutObjectResult putResult = getOSSClient().putObject(getUploadInfo().getOssBucketName(), key.toString(), is, metadata);
        //解析结果
        System.out.println("md5码为"+putResult.getETag());
        rescource = new Rescource();
        rescource.setFileName(realName.toString());
        rescource.setFileSize(new java.text.DecimalFormat("#.##").format(file.getSize()/1024)+"kb");
        rescource.setHash(hash);
        rescource.setFileType(StringUtils.isBlank(fileExtension)?"unknown":fileExtension);
        rescource.setWebUrl(returnUrl.toString());
        rescource.setSource("oss");
        rescource.insert();
        getOSSClient().shutdown();
        is.close();
    } catch (Exception e) {
        throw new MyException("上传阿里云OSS服务器异常." + e.getMessage());
    }
    return returnUrl.toString();
}
 
Example #23
Source File: OssUtil.java    From feeyo-hlsserver with Apache License 2.0 4 votes vote down vote up
/**
 * 获取object头信息
 */
public ObjectMetadata getObjectMetadata(String fileName, long streamId) {
	ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName,
			String.valueOf(streamId) + "/" + fileName);
	return objectMetadata;
}
 
Example #24
Source File: OssUtil.java    From feeyo-hlsserver with Apache License 2.0 4 votes vote down vote up
/**
 * 获取object的大小
 */
public long getObjectLength(String fileName, long streamId) {
	ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName,
			String.valueOf(streamId) + "/" + fileName);
	return objectMetadata.getContentLength();
}
 
Example #25
Source File: HlsVodHandler.java    From feeyo-hlsserver with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
	
    HttpRequest request = (DefaultHttpRequest) e.getMessage();
    if ( !request.getUri().matches(regex) ) {
        LOGGER.warn("bad request: " + request.getUri());
        HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
        return;
    }

    
    String[] path = request.getUri().split("/");
    String alias = path[3];
    final String reqFileName = path[4];
    
    Long streamId = HlsLiveStreamMagr.INSTANCE().getStreamIdByAlias(alias);
    if( streamId == null ) {
        LOGGER.warn(" aac vod, lookup alias failed: " + alias);
        HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
        return;
    }
    
    
    OssUtil ossOperation = new OssUtil();
    byte[] content = null;

    if (reqFileName.endsWith(".m3u8")) {
    	
        if ( !ossOperation.doesObjectExist(reqFileName, streamId) ) {
        	
            boolean needWaite = false;
            // the very first listener of a specific m3u8 will create the m3u8 file and the ts file
            // the reset will be in a waiter set
            synchronized (m3u8WaiteSet) {
                if (!m3u8WaiteSet.add(reqFileName)) {
                    needWaite = true;
                }
            }

            if (needWaite) {
                synchronized (m3u8WaiteSet) {
                    while (m3u8WaiteSet.contains(reqFileName)) {
                        m3u8WaiteSet.wait(1000);
                    }
                }
            } else {
                content = generateTsFiles(reqFileName, streamId);
            }
        }
    }

    if (content == null) {
        content = cachedVodTsFiles.get(reqFileName);
        if (content == null) {
            if (ossOperation.doesObjectExist(reqFileName, streamId)){
                InputStream inputStream = ossOperation.readObject(reqFileName, streamId);

                ObjectMetadata objectMetadata = ossOperation.getObjectMetadata(reqFileName,streamId);
                int len = (int)objectMetadata.getContentLength();
                content = new byte[len];

                int writePtr = 0;
                for (;;) {
                    int ret = inputStream.read(content, writePtr,len-writePtr);
                    if ((ret == -1) || (writePtr += ret) >= len)
                        break;
                }
            } else {
                LOGGER.warn("request file not on OSS: " + request.getUri());
                HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
                return;
            }
        }
    }

    ossOperation.closeOSSClient();

    long timeMillis = System.currentTimeMillis();

    DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.headers().set(HttpHeaders.Names.DATE, HttpUtil.getDateString(timeMillis));
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, HttpUtil.getMimeType(reqFileName));
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.length);
    response.headers().set(HttpHeaders.Names.LAST_MODIFIED, HttpUtil.getDateString(timeMillis));
    response.headers().set(HttpHeaders.Names.EXPIRES, HttpUtil.getDateString(timeMillis + VOD_CACHE_TIME));
    response.headers().set(HttpHeaders.Names.CACHE_CONTROL, "max-age=" + (VOD_CACHE_TIME/1000));

    response.setContent(ChannelBuffers.copiedBuffer(content));

    e.getChannel().write(response);
}
 
Example #26
Source File: OssConcurrentGetObjectTests.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
	/*
	 * Constructs a client instance with your account for accessing OSS
	 */
	client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

	try {
		/*
		 * Upload an object to your bucket
		 */
		System.out.println("Uploading a new object to OSS from a file\n");
		client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

		/*
		 * Get size of the object and pre-create a random access file to
		 * hold object data
		 */
		ObjectMetadata metadata = client.getObjectMetadata(bucketName, key);
		long objectSize = metadata.getContentLength();
		RandomAccessFile raf = new RandomAccessFile(localFilePath, "rw");
		raf.setLength(objectSize);
		raf.close();

		/*
		 * Calculate how many blocks to be divided
		 */
		final long blockSize = 1 * 1024 * 1024L; // 1MB
		int blockCount = (int) (objectSize / blockSize);
		if (objectSize % blockSize != 0) {
			blockCount++;
		}
		System.out.println("Total blocks count " + blockCount + "\n");

		/*
		 * Download the object concurrently
		 */
		System.out.println("Start to download " + key + "\n");
		for (int i = 0; i < blockCount; i++) {
			long startPos = i * blockSize;
			long endPos = (i + 1 == blockCount) ? objectSize : (i + 1) * blockSize;
			executorService.execute(new BlockFetcher(startPos, endPos, i + 1));
		}

		/*
		 * Waiting for all blocks finished
		 */
		executorService.shutdown();
		while (!executorService.isTerminated()) {
			try {
				executorService.awaitTermination(5, TimeUnit.SECONDS);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		/*
		 * Verify whether all blocks are finished
		 */
		if (completedBlocks.intValue() != blockCount) {
			throw new IllegalStateException("Download fails due to some blocks are not finished yet");
		} else {
			System.out.println("Succeed to download object " + key);
		}

	} catch (OSSException oe) {
		System.out.println("Caught an OSSException, which means your request made it to OSS, "
				+ "but was rejected with an error response for some reason.");
		System.out.println("Error Message: " + oe.getErrorMessage());
		System.out.println("Error Code:       " + oe.getErrorCode());
		System.out.println("Request ID:      " + oe.getRequestId());
		System.out.println("Host ID:           " + oe.getHostId());
	} catch (ClientException ce) {
		System.out.println("Caught an ClientException, which means the client encountered "
				+ "a serious internal problem while trying to communicate with OSS, "
				+ "such as not being able to access the network.");
		System.out.println("Error Message: " + ce.getMessage());
	} finally {
		/*
		 * Do not forget to shut down the client finally to release all
		 * allocated resources.
		 */
		if (client != null) {
			client.shutdown();
		}
	}
}