// https://github.com/javaswift/joss/blob/master/src/main/java/org/javaswift/joss/model/StoredObject.java @Override public void storeFile(MultipartFile myFile, String fileId, int fileTTL) throws IOException { if (swiftUsername == null) { System.out.println("Swift username is not configured"); } assert swiftUsername != null; if (config == null) { login(); } StoredObject swiftObject = container.getObject(fileId); swiftObject.uploadObject(myFile.getInputStream()); if (myFile.getContentType() != null) { swiftObject.setContentType(myFile.getContentType()); } Map<String, Object> metadata = new HashMap<String, Object>(); if (myFile.getOriginalFilename() != null) { metadata.put("filename", myFile.getOriginalFilename()); } if (myFile.getContentType() != null) { metadata.put("content-type", myFile.getContentType()); } swiftObject.setMetadata(metadata); swiftObject.saveMetadata(); // swiftObject.setDeleteAt(Date date); }
@Test public void deleteObject() throws IOException { object.uploadObject(new byte[] {}); assertEquals(1, container.list().size()); object.delete(); assertEquals(0, container.list().size()); }
@Override public Upload getById(String fileId) throws IOException { if (config == null) { login(); } Upload uploadRec = new Upload(); StoredObject swiftObject = null; try { swiftObject = container.getObject(fileId); } catch (Exception e) { throw new FileNotFoundException(fileId); } Map<String, Object> metadata = swiftObject.getMetadata(); uploadRec.setExpires(swiftObject.getDeleteAtAsDate()); uploadRec.setFilename((String) metadata.get("filename")); uploadRec.setContentType((String) metadata.get("content-type")); uploadRec.setSize(swiftObject.getContentLength()); Date today = new Date(System.currentTimeMillis()); if (uploadRec.getExpires() != null) { if (today.after(uploadRec.getExpires())) { throw new FileNotFoundException(fileId); } } uploadRec.setContentStream(swiftObject.downloadObjectAsInputStream()); return uploadRec; }
@Test public void setAndDoNotSaveMetadata() throws IOException { expectStatusCode(202); object.setAndDoNotSaveMetadata("Year", "1989"); object.setAndDoNotSaveMetadata("Company", "42 BV"); object.saveMetadata(); verifyHeaderValue("1989", X_OBJECT_META_PREFIX + "Year"); verifyHeaderValue("42 BV", X_OBJECT_META_PREFIX + "Company"); }
/** * Delete an object if it exists. * * @param object object handle to delete * @return true if object deletion was successful */ private boolean deleteObject(final StoredObject object) { try { object.delete(); return true; } catch (NotFoundException e) { LOG.debug("Object {} not found", object.getPath()); } return false; }
@Test public void getObject() throws IOException { StoredObject object1 = container.getObject("some-object"); assertFalse(object1.exists()); object1.uploadObject(new byte[] {0x01}); StoredObject object2 = container.getObject("some-object"); assertEquals(object1, object2); assertTrue(object1.exists()); }
@Test public void emptyDeleteAt() { expectStatusCode(202); List<Header> headers = new ArrayList<Header>(); prepareHeader(response, LAST_MODIFIED, "Mon, 03 Sep 2012 05:40:33 GMT", headers); prepareHeadersForRetrieval(response, headers); assertNull(object.getDeleteAt()); assertNull(object.getDeleteAtAsDate()); }
@Test public void removeAndDoNotSaveMetadata() throws IOException { expectStatusCode(202); object.removeAndDoNotSaveMetadata("Year"); object.removeAndDoNotSaveMetadata("Company"); object.saveMetadata(); verifyHeaderValue("", X_OBJECT_META_PREFIX + "Year"); verifyHeaderValue("", X_OBJECT_META_PREFIX + "Company"); }
@Test public void setLastModified() { String dateText = "2012-12-05T14:57:00.165930"; StoredObject object = createStoredObject("alpha"); object.setLastModified(dateText); object.metadataSetFromHeaders(); Date date = object.getLastModifiedAsDate(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); assertEquals("2012-12-05T14:57:00", formatter.format(date)); }
@Override public boolean deleteById(String fileId) throws IOException { try { StoredObject swiftObject = container.getObject(fileId); swiftObject.delete(); return true; } catch (NotFoundException e) { return false; } }
@Test public void compareObjectsWithSameNameInDifferentContainers() { Container container1 = account.getContainer("alpha"); StoredObject object1 = container1.getObject("img1.png"); Container container2 = account.getContainer("beta"); StoredObject object2 = container2.getObject("img1.png"); assertNotSame(object1, object2); assertEquals(-1, object1.compareTo(object2)); assertEquals(1, object2.compareTo(object1)); }
/** * Delete an arbitrary BlobPath from our store. * * @param path The blob path to delete */ @Override public void delete(BlobPath path) { String keyPath = path.buildAsString("/"); if (!keyPath.isEmpty()) { keyPath = keyPath + "/"; } StoredObject obj = swift().getObject(keyPath); if (obj.exists()) { obj.delete(); } }
@Test public void listObjectsPaged() { container.getObject("A").uploadObject(new byte[] {}); container.getObject("B").uploadObject(new byte[] {}); StoredObject object3 = container.getObject("C"); object3.uploadObject(new byte[] {}); StoredObject object4 = container.getObject("D"); object4.uploadObject(new byte[] {}); Collection<StoredObject> objects = container.list(null, "B", 2); assertEquals(2, objects.size()); objects.contains(object3); objects.contains(object4); }
@Test public void listContainersUsePaginationMap() { container.getObject("A").uploadObject(new byte[] {}); container.getObject("B").uploadObject(new byte[] {}); StoredObject object3 = container.getObject("C"); object3.uploadObject(new byte[] {}); StoredObject object4 = container.getObject("D"); object4.uploadObject(new byte[] {}); PaginationMap paginationMap = container.getPaginationMap(2); Collection<StoredObject> objects = container.list(paginationMap, 1); assertEquals(2, objects.size()); objects.contains(object3); objects.contains(object4); }
@Test public void uploadFromByteArray() throws IOException { expectStatusCode(201); this.bytes = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05}; object.uploadObject(this.bytes); verifyUploadContent(this.bytes); }
@Test public void removeDeleteAt() throws IOException, DateParseException { expectStatusCode(202, false); prepareMetadata(); object.setDeleteAt(null); verifyHeaderValue(null, X_DELETE_AT, "POST"); }
@Test public void setContentType() throws IOException { expectStatusCode(202, false); prepareMetadata(); object.setContentType("image/bmp"); verifyHeaderValue("image/bmp", CONTENT_TYPE, "POST"); }
@Test(expected = CommandException.class) public void getPublicUrlThrowsException() { Container container = account.getContainer("alpha"); object = container.getObject(null); expectPublicUrl("http://static.resource.com"); assertEquals( "http://static.resource.com/alpha/a+n%C3%A4m%C3%BC+with+spaces.png", object.getPublicURL()); }
@Test public void copyObject() throws IOException { Container beta = account.getContainer("beta"); StoredObject object2 = beta.getObject("other-image.png"); expectStatusCode(201); object.copyObject(beta, object2); verifyHeaderValue("/alpha/image.png", CopyFrom.X_COPY_FROM); }
@Test public void setDeleteAt() throws IOException, DateParseException { expectStatusCode(202, false); prepareMetadata(); final Date date = DateUtils.parseDate("Mon, 03 Sep 2001 05:40:33 GMT"); object.setDeleteAt(date); verifyHeaderValue(Long.toString(date.getTime() / 1000), X_DELETE_AT, "POST"); }
@Test public void setDeleteAfter() throws IOException { expectStatusCode(202, false); prepareMetadata(); object.setDeleteAfter(42); verifyHeaderValue("42", X_DELETE_AFTER, "POST"); verifyHeaderValue(null, X_DELETE_AT, "POST"); }
@Test public void uploadFromFile() throws IOException { OutputStream outputStream = new FileOutputStream(downloadedFile); IOUtils.write(this.bytes, outputStream); outputStream.close(); expectStatusCode(201); object.uploadObject(downloadedFile); verifyUploadContent(this.bytes); }
@Test public void getPublicUrlEncoded() { Container container = account.getContainer("alpha"); object = container.getObject("a n\u00e4m\u00fc with spaces.png"); expectPublicUrl("http://static.resource.com"); assertEquals( "http://static.resource.com/alpha/a%20n%C3%A4m%C3%BC%20with%20spaces.png", object.getPublicURL()); }
@SuppressWarnings("EqualsBetweenInconvertibleTypes") @Test public void compareContainers() { Container container = account.getContainer("alpha"); StoredObject object1 = container.getObject("img1.png"); StoredObject object2 = container.getObject("img2.png"); assertFalse(object1.equals("alpha")); assertFalse(object1.equals(object2)); Map<StoredObject, String> containers = new TreeMap<StoredObject, String>(); containers.put(object1, object1.getName()); containers.put(object2, object2.getName()); assertEquals(object1.getName(), containers.get(object1)); assertEquals(object2.getName(), containers.get(object2)); assertEquals(object1.getName().hashCode(), object1.hashCode()); }
@Test public void getMetadata() throws IOException, DateParseException { expectStatusCode(202); prepareMetadata(); assertEquals("1989", object.getMetadata().get("Year")); assertEquals("42 BV", object.getMetadata().get("Company")); assertEquals("Mon, 03 Sep 2012 05:40:33 GMT", object.getLastModified()); assertEquals( DateUtils.parseDate("Mon, 03 Sep 2012 05:40:33 GMT"), object.getLastModifiedAsDate()); assertEquals(654321, object.getContentLength()); assertEquals("image/png", object.getContentType()); assertEquals("cae4ebb15a282e98ba7b65402a72f57c", object.getEtag()); assertEquals("1339429105", object.getDeleteAt()); assertEquals(DateUtils.parseDate("Mon, 11 Jun 2012 15:38:25 GMT"), object.getDeleteAtAsDate()); }
@Test public void setMetadata() throws IOException { expectStatusCode(202); Map<String, Object> metadata = new TreeMap<String, Object>(); metadata.put("Year", "1989"); metadata.put("Company", "42 BV"); object.setMetadata(metadata); verifyHeaderValue("1989", X_OBJECT_META_PREFIX + "Year"); verifyHeaderValue("42 BV", X_OBJECT_META_PREFIX + "Company"); }
public void DownloadUsingSWIFT() throws Exception { while (true) { Collection<StoredObject> list = swiftapi.ReadBucket(SWIFT_ACCESS_KEY_ID, SWIFT_SECRET_KEY, SWIFT_ENDPOINT, SWIFT_BUCKET); int scount = list.size(); int localCount = new File(LOCAL_DIR).listFiles().length; if (scount != localCount) { for (StoredObject obj : list) { if (!(new File(LOCAL_DIR, obj.getName()).exists())) { System.out.println("Downloading - " + obj.getName()); vLogger.LogInfo("emcWorldDownloader: Downloading - " + obj.getName()); InputStream in = swiftapi.ReadObject( SWIFT_ACCESS_KEY_ID, SWIFT_SECRET_KEY, SWIFT_ENDPOINT, SWIFT_BUCKET, obj.getName()); File file = new File(LOCAL_DIR + obj.getName()); int count = 0; byte[] buf = new byte[1024]; OutputStream out = new FileOutputStream(file); while ((count = in.read(buf)) != -1) { if (Thread.interrupted()) { throw new InterruptedException(); } out.write(buf, 0, count); } out.close(); in.close(); } else { System.out.println("Skipping - " + obj.getName()); vLogger.LogInfo("emcWorldDownloader: Skipping - " + obj.getName()); } } } else System.out.println("No New Files Yet"); vLogger.LogInfo("emcWorldDownloader: Skipping - Sleep 60 seconds"); System.out.println("Sleep for 1 minute"); Thread.sleep(60000); } }
@Test public void getBareName() { Container container = account.getContainer("alpha"); object = container.getObject("/abc/def/image.png"); assertEquals("image.png", object.getBareName()); }
@Test public void whatItIs() { assertTrue(object.isObject()); assertFalse(object.isDirectory()); }
@Test(expected = UnsupportedOperationException.class) public void getAsDirectory() { object.getAsDirectory(); }