Dropbox API 사용법
iOS Ad-hoc 버전 릴리즈 및 팀의 파일 공유를 위해 Dropbox
를 사용하는데 이번에 CI(continuous integration)를 하려고 마음을 먹어서 단계별 기능을 확인하고 있다.
Dropbox API 사용법
https://www.dropbox.com/developers-v1/core/start/java 링크에 상세하게 설명되어 있다.
위의 단계에서 주의해야 할 사항은 AccessToken을 얻는 부분이다. 예제대로 따라 하면 AccessToken을 매번 새로 생성하기 때문에 재사용이 안된다.
그렇기 때문에 App Home – Settings – Generated access token 을 이용하여 AccessToken을 생성한다.
[DropboxAPI.java]
import com.dropbox.core.DbxClient; import com.dropbox.core.DbxEntry; import com.dropbox.core.DbxException; import com.dropbox.core.DbxWriteMode; import java.io.*; public class DropboxAPI { public DbxEntry.File uploadFile(DbxClient client, String fileName, long fileLength, byte[] fileBytes) throws IOException, DbxException { ByteArrayInputStream bais = new ByteArrayInputStream(fileBytes); DbxEntry.File uploadedFile = client.uploadFile("/CoozplzFile/" + fileName, DbxWriteMode.add(), fileLength, bais); return uploadedFile; } public DbxEntry.WithChildren listAllFiles(DbxClient client) throws DbxException { DbxEntry.WithChildren children = client.getMetadataWithChildren("/CoozplzFile"); return children; } public DbxEntry.File downloadFile(DbxClient client, String sourceFile, File downloadedPath) throws IOException, DbxException { FileOutputStream fos = new FileOutputStream(downloadedPath); DbxEntry.File downloadedFile = client.getFile("/CoozplzFile/"+ sourceFile, null, fos); fos.close(); return downloadedFile; } }
[DropboxAPITest.java]
import com.dropbox.core.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import java.io.*; import java.util.List; import java.util.Locale; public class DropboxAPITest { public static String ACCESS_TOKEN = "ACCESS_TOKEN"; DbxClient client; @Before public void setup() { DbxRequestConfig config = new DbxRequestConfig("Sample/1.0", Locale.getDefault().toString()); client = new DbxClient(config, ACCESS_TOKEN); } @Test public void testListFiles() throws DbxException { DropboxAPI dropboxAPI = new DropboxAPI(); DbxEntry.WithChildren children = dropboxAPI.listAllFiles(client); Assert.assertNotNull(children); List<DbxEntry> list = children.children; System.out.println(list); Assert.assertNotNull(list); } @Test public void testUpload() throws IOException, DbxException { DropboxAPI dropboxAPI = new DropboxAPI(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream in = DropboxAPITest.class.getResourceAsStream("101-N101_c.ai"); byte[] buf = new byte[1024]; while (true) { int readSize = in.read(buf); if (readSize < 0) { break; } baos.write(buf, 0, readSize); } DbxEntry.File uploadedFile = dropboxAPI.uploadFile(client, "101-N101_c.ai", baos.size(), baos.toByteArray()); Assert.assertEquals(baos.size(), uploadedFile.numBytes); System.out.println("Numbers of bytes = " + uploadedFile.numBytes); baos.close(); } @Test public void testDownload() throws IOException, DbxException { DropboxAPI dropboxAPI = new DropboxAPI(); DbxEntry.File file = dropboxAPI.downloadFile(client, "101-N101_c.ai", new File("D:/101-N101_c.ai")); String url = client.createShareableUrl(file.path); System.out.println(url); Assert.assertTrue(url.contains("https://www")); } }