有谁使用过阿里云oss的吗?多文件多图时怎么上传
前段时间正好做个项目,就是用的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 = SysConst.getProperty("OSS_ACCESS_KEY_ID");ACCESS_KEY_SECRET = SysConst.getProperty("OSS_ACCESS_KEY_SECRET");ENDPOINT = SysConst.getProperty("OSS_ENDPOINT"); BUCKETNAME = SysConst.getProperty("OSS_BUCKETNAME"); BUCKETNAME_VIDEO = SysConst.getProperty("OSS_BUCKETNAME_VIDEO"); OUT_OF_DATE = SysConst.getProperty("OSS_OUT_OF_DATE");} catch (java.lang.Exception e) {e.printStackTrace();}}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 meta.setContentType(mimeType); // 上传Object. LogUtil.printConsoleLog("*****************上传图片到oss服务器开始*****************" + key); PutObjectResult result = client.putObject(BUCKETNAME, key, content, meta); LogUtil.printConsoleLog("*****************上传图片到oss服务器结束*****************" + key); return result.getETag();}/** * @Description: 根据key获取oss服务器上的图片地址 * @param key * @return * @ReturnType:String*/public String getImgURl(String key){init();Date expires = new Date (new Date().getTime() + Integer.parseInt(OUT_OF_DATE)); // 30 minute to expireGeneratePresignedUrlRequest generatePresignedUrlRequest ;if(key.startsWith("msplatform")){generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key);}else {generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key);}generatePresignedUrlRequest.setExpiration(expires);URL url = client.generatePresignedUrl(generatePresignedUrlRequest);return url.toString();}/** * @Description:根据key获取oss服务器上的ipa文件地址 * @param key * @return * @ReturnType:String */public String getIpaURl(String key){init();Date expires = new Date(new Date().getTime()+ 10*365*24*3600*1000);GeneratePresignedUrlRequest generatePresignedUrlRequest ;if(key.startsWith("msplatform")){generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key);}else {generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key);}generatePresignedUrlRequest.setExpiration(expires);URL url = client.generatePresignedUrl(generatePresignedUrlRequest);return url.toString();}/** * @Description: 根据key获取oss服务器上的图片地址 * @param key * @return * @ReturnType:String*/public InputStream getObject(String key){init();OSSObject object = null;if(key.startsWith("msplatform")){object= client.getObject(BUCKETNAME, key);}else{object= client.getObject(BUCKETNAME_VIDEO, key);}object.getObjectMetadata().getContentType();// 获取Object的输入流InputStream objectContent = object.getObjectContent();return objectContent;}/** * @Description:删除文件 * @param key OSS文件服务器上文件的唯一标识 * @ReturnType:void*/public void deleteFile(String key){init();client.deleteObject(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 meta.setContentLength(position); meta.setContentType(mimeType); meta.setCacheControl("no-cache"); meta.setContentEncoding("utf-8"); // 上传 LogUtil.printConsoleLog("*****************断点上传图片到oss服务器开始*****************" + key); AppendObjectRequest appendObjectRequest = new AppendObjectRequest(BUCKETNAME, key, content, meta); appendObjectRequest.setPosition(Long.valueOf(position)); AppendObjectResult appendObjectResult =client.appendObject(appendObjectRequest); LogUtil.printConsoleLog("*****************断点上传图片到oss服务器结束*****************" + key); return appendObjectResult.getNextPosition().toString();}public static void main(String[] args) {OssManage ossManage = new OssManage();}}
怎么上传图片到阿里云?
可以通过FTP的形式进行传输。或者是你做好的一个网站,把图片进行上传到网站,这样也到了阿里云的服务器下了。
android 阿里云 对象存储objectkey 是啥玩意
your-object-key就是你希望上传的文件在OSS中的名称,可以用“/"分割。比如:
bucket:mybucket,key=image/2014/1.jpg
上传成功后,对应的OSS域名就是
阿里云oss上传文件怎么返回上传文件的url
阿里云OSS中url中可以实现授权文件上传吗
Oss中url中授权文件上传可以实现
java版代码示例如下:GeneratePresignedUrlRequest generatePresignedUrlRequest =
1)url中包含签名的好像能够实现文件的下载,阿里云能实现url中授权文件上传吗?
new GeneratePresignedUrlRequest(bucketName, key);
2)java版代码示例如下:
generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
generatePresignedUrlRequest.setExpiration(expires);
generatePresignedUrlRequest.addUserMetadata("usermeta", "uservalue"); // If you need to set user metadata
URL url = client.generatePresignedUrl(generatePresignedUrlRequest);