require: {
"johnlui/aliyun-oss": "dev-master"
}
然后运行 composer update。
使用
use JohnLui\AliyunOSS\AliyunOSS;
// 构建 OSSClient 对象
// 三个参数:服务器地址、阿里云提供的AccessKeyId、AccessKeySecret
$oss = AliyunOSS::boot('>
使用第三方的阿里云OOS来当文件服务器使用,稳定性相对来说比较高,容灾也方便。
官方参考地址: >前段时间正好做个项目,就是用的oss,普通上传和断点续传,代码贴出来,看是不是你需要的public class OssManage {public static String ACCESS_KEY_ID = null;public static String ACCESS_KEY_SECRET = null;public static String ENDPOINT = null;public static String BUCKETNAME = null;public static String BUCKETNAME_VIDEO = null;public static String OUT_OF_DATE = null;private OSSClient client = null;private ObjectMetadata meta = null;static{try {ACCESS_KEY_ID = SysConstgetProperty("OSS_ACCESS_KEY_ID");ACCESS_KEY_SECRET = SysConstgetProperty("OSS_ACCESS_KEY_SECRET");ENDPOINT = SysConstgetProperty("OSS_ENDPOINT"); BUCKETNAME = SysConstgetProperty("OSS_BUCKETNAME"); BUCKETNAME_VIDEO = SysConstgetProperty("OSS_BUCKETNAME_VIDEO"); OUT_OF_DATE = SysConstgetProperty("OSS_OUT_OF_DATE");} catch (javalangException e) {eprintStackTrace();}}public void init(){ // 初始化一个OSSClient client = new OSSClient(ENDPOINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET); meta = new ObjectMetadata();}/ @Description: 上传文件到OSS文件服务器 @param content 文件流 @param key 上传为OSS文件服务器的唯一标识 @param mimeType 文档类型 @throws Exception @ReturnType:String OSSObject的ETag值。/public String uploadFile(InputStream content,String key,String mimeType) throws Exception{//进行初始化init(); // 必须设置ContentLength metasetContentType(mimeType); // 上传Object LogUtilprintConsoleLog("上传到oss服务器开始" + key); PutObjectResult result = clientputObject(BUCKETNAME, key, content, meta); LogUtilprintConsoleLog("上传到oss服务器结束" + key); return resultgetETag();}/ @Description: 根据key获取oss服务器上的地址 @param key @return @ReturnType:String/public String getImgURl(String key){init();Date expires = new Date (new Date()getTime() + IntegerparseInt(OUT_OF_DATE)); // 30 minute to expireGeneratePresignedUrlRequest generatePresignedUrlRequest ;if(keystartsWith("msplatform")){generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key);}else {generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key);}generatePresignedUrlRequestsetExpiration(expires);URL url = clientgeneratePresignedUrl(generatePresignedUrlRequest);return urltoString();}/ @Description:根据key获取oss服务器上的ipa文件地址 @param key @return @ReturnType:String /public String getIpaURl(String key){init();Date expires = new Date(new Date()getTime()+ 103652436001000);GeneratePresignedUrlRequest generatePresignedUrlRequest ;if(keystartsWith("msplatform")){generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key);}else {generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key);}generatePresignedUrlRequestsetExpiration(expires);URL url = clientgeneratePresignedUrl(generatePresignedUrlRequest);return urltoString();}/ @Description: 根据key获取oss服务器上的地址 @param key @return @ReturnType:String/public InputStream getObject(String key){init();OSSObject object = null;if(keystartsWith("msplatform")){object= clientgetObject(BUCKETNAME, key);}else{object= clientgetObject(BUCKETNAME_VIDEO, key);}objectgetObjectMetadata()getContentType();// 获取Object的输入流InputStream objectContent = objectgetObjectContent();return objectContent;}/ @Description:删除文件 @param key OSS文件服务器上文件的唯一标识 @ReturnType:void/public void deleteFile(String key){init();clientdeleteObject(BUCKETNAME, key);}/ @Description: 断点上传文件到OSS文件服务器 @param content 文件流 @param key 上传为OSS文件服务器的唯一标识 @param position 位置/public String appendObjectFile(InputStream content,String key,int position,String mimeType) throws Exception{ //进行初始化 init(); // 必须设置ContentLength metasetContentLength(position); metasetContentType(mimeType); metasetCacheControl("no-cache"); metasetContentEncoding("utf-8"); // 上传 LogUtilprintConsoleLog("断点上传到oss服务器开始" + key); AppendObjectRequest appendObjectRequest = new AppendObjectRequest(BUCKETNAME, key, content, meta); appendObjectRequestsetPosition(LongvalueOf(position)); AppendObjectResult appendObjectResult =clientappendObject(appendObjectRequest); LogUtilprintConsoleLog("断点上传到oss服务器结束" + key); return appendObjectResultgetNextPosition()toString();}public static void main(String[] args) {OssManage ossManage = new OssManage();}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)