Java tutorial
/* * Copyright (c) 2015-2020, Chen Rui * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.vnet.demo.service; import com.microsoft.azure.storage.StorageException; import com.microsoft.azure.storage.blob.CloudBlockBlob; import com.vnet.demo.dao.StorageDao; import com.vnet.demo.entity.Storage; import com.vnet.demo.service.azure.storage.AzureStorageServiceFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; /** * Azure Storage Manage Service * access azure storage data, search, upload and download file from azure storage * * @author Chen Rui */ @Service public class AzureStorgeService { @Autowired AzureStorageServiceFactory storageSessionFactory; @Autowired StorageDao storageDao; public Storage uploadBlobImage(File file) throws IOException { String name = file.getName(); Storage storage = this.uploadBlobFile(new FileInputStream(file), name, file.length()); return storage; } public Storage uploadBlobImage(InputStream inpustream, String name, Long length) throws IOException { String newPath = storageSessionFactory.pubFileToStorage(inpustream, name, length); String type = name.substring(name.lastIndexOf(".") + 1); Storage storage = new Storage(); storage.setOriginalName(name); storage.setPath(newPath); storage.setType(type); storage.setIsPub(true); storageDao.save(storage); return storage; } public Storage uploadBlobFile(File file) throws IOException { String name = file.getName(); Storage storage = this.uploadBlobFile(new FileInputStream(file), name, file.length()); return storage; } public Storage uploadBlobFile(InputStream inpustream, String name, Long length) { String newPath = storageSessionFactory.pubFileToStorage(inpustream, name, length); String type = name.substring(name.lastIndexOf(".") + 1); Storage storage = new Storage(); storage.setOriginalName(name); storage.setPath(newPath); storage.setType(type); storage.setIsPub(false); storageDao.save(storage); return storage; } /** * check file is exsit in azure storgae contain * * @param id * @return */ public boolean checkFileExist(Long id) { Storage storage = storageDao.findById(id); try { if (storage.getIsPub()) { CloudBlockBlob blob = storageSessionFactory.getPubContainer() .getBlockBlobReference(storage.getPath()); return blob.exists(); } else { CloudBlockBlob blob = storageSessionFactory.getPrivateContainer() .getBlockBlobReference(storage.getPath()); return blob.exists(); } } catch (URISyntaxException e) { e.printStackTrace(); } catch (StorageException e) { e.printStackTrace(); } return false; } public String getPubAccessUrl() { return storageSessionFactory.getPublicAccessUrl(); } public String getPrivateAccessUrl(String containName) { return storageSessionFactory.getPrivateAccessUrl(); } }