| | |
| | | import com.azure.storage.blob.BlobContainerClient; |
| | | import com.azure.storage.blob.BlobServiceClient; |
| | | import com.azure.storage.blob.BlobServiceClientBuilder; |
| | | import com.doumee.core.utils.azure.AzureBlobUtil; |
| | | import com.obs.services.BasicObsCredentialsProvider; |
| | | import com.obs.services.ObsClient; |
| | | import com.obs.services.exception.ObsException; |
| | | import com.obs.services.model.*; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | |
| | | } |
| | | private void initClient() { |
| | | this.obsClient = new ObsClient(new BasicObsCredentialsProvider(this.accessId, this.accessKey), this.endpoint); |
| | | } |
| | | private void uploadLocalFile(File file,String bucket,String key) { |
| | | try { |
| | | if(this.obsClient == null){ |
| | | initClient(); |
| | | } |
| | | // 文件上传 |
| | | // localfile 为待上传的本地文件路径,需要指定到具体的文件名 |
| | | PutObjectRequest request = new PutObjectRequest(); |
| | | request.setBucketName(bucket); |
| | | request.setObjectKey(key); |
| | | request.setFile(file); |
| | | obsClient.putObject(request); |
| | | System.out.println("putObject successfully"); |
| | | } catch (ObsException e) { |
| | | System.out.println("putObject failed"); |
| | | // 请求失败,打印http状态码 |
| | | System.out.println("HTTP Code:" + e.getResponseCode()); |
| | | // 请求失败,打印服务端错误码 |
| | | System.out.println("Error Code:" + e.getErrorCode()); |
| | | // 请求失败,打印详细错误信息 |
| | | System.out.println("Error Message:" + e.getErrorMessage()); |
| | | // 请求失败,打印请求id |
| | | System.out.println("Request ID:" + e.getErrorRequestId()); |
| | | System.out.println("Host ID:" + e.getErrorHostId()); |
| | | e.printStackTrace(); |
| | | } catch (Exception e) { |
| | | System.out.println("putObject failed"); |
| | | // 其他异常信息打印 |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | ObsUtil blobUtil = new ObsUtil("HPUAQVBRXX9A9TLZ3RTA","uHC2uoFh42Z2xgQmCBBtG8rNZ4Caf85qQ2DQqZZf","obs.cn-south-1.myhuaweicloud.com"); |
| | | blobUtil.uploadLocalFile(new File("D://static/1.png"),"jinkuai","identity/1.png"); |
| | | blobUtil.uploadLocalFile(new File("D://static/2.png"),"jinkuai","identity/2.png"); |
| | | blobUtil.uploadLocalFile(new File("D://static/3.png"),"jinkuai","identity/3.png"); |
| | | blobUtil.uploadLocalFile(new File("D://static/4.png"),"jinkuai","identity/4.png"); |
| | | } |
| | | public static Integer uploadNetFile(ObsClient obsClient,String bucketName,String url, String objectKey) throws IOException { |
| | | InputStream is = new URL(url).openStream(); |
| | | if(is != null){ |