public void saveFile(MultipartFile file, String path, String fileName) throws IOException { String bucketPath = "s3://" + bucket + "/"; Resource resource = this.resourceLoader.getResource(bucketPath+path+ "/"+ fileName); System.out.println("Storing File :"+bucketPath+path+ "/"+ fileName); WritableResource writableResource = (WritableResource) resource; try (OutputStream outputStream = writableResource.getOutputStream()) { byte[] bytes = IOUtils.toByteArray(file.getInputStream()); logger.info("Storing file {} in S3", fileName); outputStream.write(bytes); }catch (Exception e){ logger.error("Failed to upload file on S3"); e.printStackTrace(); } }
protected void handleMultipart(String store, String path, MultipartFile multiPart) throws HttpRequestMethodNotSupportedException, IOException { ContentStoreInfo info = ContentStoreUtils.findStore(storeService, store); if (info == null) { throw new IllegalArgumentException("Not a Store"); } String pathToUse = path.substring(store.length() + 1); Resource r = ((Store)info.getImpementation()).getResource(pathToUse); if (r == null) { throw new ResourceNotFoundException(); } if (r instanceof WritableResource == false) { throw new UnsupportedOperationException(); } InputStream in = multiPart.getInputStream(); OutputStream out = ((WritableResource)r).getOutputStream(); IOUtils.copy(in, out); IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); }
@Test public void testUploadFileWithRelativePath() throws Exception { String bucketName = this.stackResourceRegistry.lookupPhysicalResourceId("EmptyBucket"); uploadFileTestFile(bucketName, "testUploadFileWithRelativePathParent", "hello world"); Resource resource = this.resourceLoader.getResource(S3_PREFIX + bucketName + "/testUploadFileWithRelativePathParent"); assertTrue(resource.exists()); WritableResource childFileResource = (WritableResource) resource.createRelative("child"); try (OutputStream outputStream = childFileResource.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(outputStream)) { writer.write("hello world"); } this.createdObjects.add(childFileResource.getFilename()); InputStream inputStream = childFileResource.getInputStream(); assertNotNull(inputStream); assertEquals("hello world", FileCopyUtils.copyToString(new InputStreamReader(inputStream, "UTF-8"))); assertEquals("hello world".length(), childFileResource.contentLength()); }
@Test public void testBucketResourceStatuses() throws IOException { Assert.assertFalse(this.bucketResource.isOpen()); Assert.assertFalse(this.bucketResource.isReadable()); Assert.assertFalse(((WritableResource) this.bucketResource).isWritable()); Assert.assertTrue(this.bucketResource.exists()); }
@Test public void testWritable() throws Exception { Assert.assertTrue(this.remoteResource instanceof WritableResource); WritableResource writableResource = (WritableResource) this.remoteResource; Assert.assertTrue(writableResource.isWritable()); writableResource.getOutputStream(); }
@RequestMapping(value = "/", method = RequestMethod.POST) String writeGcs(@RequestBody String data) throws IOException { try (OutputStream os = ((WritableResource) this.gcsFile).getOutputStream()) { os.write(data.getBytes()); } return "file was updated\n"; }
@StoreType("store") @RequestMapping(value = BASE_MAPPING, method = RequestMethod.PUT, headers={"content-type!=multipart/form-data", "accept!=application/hal+json"}) @ResponseBody public void putContent(HttpServletRequest request, HttpServletResponse response, @PathVariable String store) throws IOException, HttpRequestMethodNotSupportedException, InstantiationException, IllegalAccessException { ContentStoreInfo info = ContentStoreUtils.findStore(storeService, store); if (info == null) { throw new IllegalArgumentException("Not a Store"); } String path = new UrlPathHelper().getPathWithinApplication(request); String pathToUse = path.substring(ContentStoreUtils.storePath(info).length() + 1); Resource r = ((Store)info.getImpementation()).getResource(pathToUse); if (r == null) { throw new ResourceNotFoundException(); } if (r instanceof WritableResource == false) { throw new UnsupportedOperationException(); } InputStream in = request.getInputStream(); OutputStream out = ((WritableResource)r).getOutputStream(); IOUtils.copy(in, out); IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); }
@PostConstruct public void resourceAccess() throws IOException { String location = "s3://" + this.bucket + "/file.txt"; WritableResource writeableResource = (WritableResource) this.resourceLoader .getResource(location); FileCopyUtils.copy("Hello World!", new OutputStreamWriter(writeableResource.getOutputStream())); Resource resource = this.resourceLoader.getResource(location); System.out.println(FileCopyUtils .copyToString(new InputStreamReader(resource.getInputStream()))); }
protected S3DeletableResource(WritableResource resource, AmazonS3 amazonS3, String location, String bucket, String root) { this.resource = resource; this.amazonS3 = amazonS3; this.location = location; this.bucket = bucket; this.root = root; }
@Override public Resource createRelative(String relativePath) throws IOException { final Resource relative = resource.createRelative(relativePath); if (relative instanceof WritableResource) { return new S3DeletableResource((WritableResource) relative, amazonS3, location, bucket, root); } else { return relative; } }
@Override public DeletableResource[] getResources(String locationPattern) throws IOException { final Resource[] resources = delegate.getResources(locationPattern); return stream(resources) // .map(resource -> convert((WritableResource) resource)) // .collect(Collectors.toList()) // .toArray(new DeletableResource[0]); }
@Override public Object convertValue(Parameterized parameterized, Class targetType, String src) { if (!Resource.class.isAssignableFrom(targetType)) { return super.convertValue(parameterized, targetType, src); } boolean out = WritableResource.class.isAssignableFrom(targetType); if (src.equals(STD_STREAM_ARG)) { return (out ? new StandardOutputResource() : new StandardInputResource()); } if (!ResourcePatternUtils.isUrl(src) && !StringUtils.startsWithAny(src, ResourceUtils.FILE_URL_PREFIX, ResourceUtils.JAR_URL_PREFIX)) { src = ResourceUtils.FILE_URL_PREFIX + src; } try { ResourceSource resourceSrc = CliValidatorServiceImpl.this.resourceSrcResolver.resolve(src); if (resourceSrc == null) { throw new CliValidatorException(String.format("Unable to resolve resource: path=%s", src)); } return resourceSrc.getResource(); } catch (IOException e) { throw new CliValidatorException(String.format("Unable to parse resource command line option (names=[%s], value=%s).", StringUtils.join(parameterized.getWrappedParameter().names(), ", "), src), e); } }
@Test public void testUploadFileWithMoreThenFiveMegabytes() throws Exception { String bucketName = this.stackResourceRegistry.lookupPhysicalResourceId("EmptyBucket"); Resource resource = this.resourceLoader.getResource(S3_PREFIX + bucketName + "/testUploadFileWithMoreThenFiveMegabytes"); assertTrue(WritableResource.class.isInstance(resource)); WritableResource writableResource = (WritableResource) resource; OutputStream outputStream = writableResource.getOutputStream(); for (int i = 0; i < (1024 * 1024 * 6); i++) { outputStream.write("c".getBytes("UTF-8")); } outputStream.close(); this.createdObjects.add("testUploadFileWithMoreThenFiveMegabytes"); }
@Test(expected = UnsupportedOperationException.class) public void testBucketOutputStream() throws IOException { ((WritableResource) this.bucketResource).getOutputStream(); }
@Override protected DeletableResource convert(WritableResource writableResource) { return new S3DeletableResource(writableResource, amazonS3, writableResource.getFilename(), bucket.getBucketName(), bucket.getRoot()); }
LocalDeletableResource(ResourceResolver resolver, WritableResource resource) { this.resolver = resolver; this.resource = resource; }
@Override public Resource createRelative(String relativePath) throws IOException { return new LocalDeletableResource(resolver, (WritableResource) resource.createRelative(relativePath)); }
@Override protected DeletableResource convert(WritableResource writableResource) { return new LocalDeletableResource(this, writableResource); }
@Override public DeletableResource getResource(String location) { return convert((WritableResource) delegate.getResource(location)); }
public ResourceResult(WritableResource resource) throws IOException { this(resource, null); }
public WritableResource getResource() { return this.resource; }
@Nullable public WritableResource getOutputFile();
@Nullable @Override public WritableResource getOutputFile() { return this.outFile; }
@Override @Parameter(names = { "-o", "--out-file" }, arity = 1, description = "Output file or \"-\" for standard output.") public void setOutputFile(@Nullable WritableResource outFile) { this.outFile = outFile; }
public ResourceResult(WritableResource resource, @Nullable String sysId) throws IOException { super(resource.getOutputStream()); this.resource = resource; this.setSystemId(((sysId != null) ? sysId : this.resource.getURL().toExternalForm())); }
protected abstract DeletableResource convert(WritableResource writableResource);
public void setOutputFile(@Nullable WritableResource outFile);