无法使用Elasticbean中的Jhipster将生产中的文件上传到s3,但在本地工作

时间:2017-06-23 08:59:27

标签: amazon-s3 elastic-beanstalk jhipster

我正在尝试使用elasticbean将文件上传到s3。它在本地工作,但在生产中得到下一个错误:

java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
at com.ruano.erp.storage.FileSystemStorageService.store(FileSystemStorageService.java:73)
at com.ruano.erp.web.rest.FileUploadResource.uploadFile(FileUploadResource.java:48)
at com.ruano.erp.web.rest.FileUploadResource$$FastClassBySpringCGLIB$$3a8f87c4.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at com.ryantenney.metrics.spring.TimedMethodInterceptor.invoke(TimedMethodInterceptor.java:48)
at com.ryantenney.metrics.spring.TimedMethodInterceptor.invoke(TimedMethodInterceptor.java:34)
at com.ryantenney.metrics.spring.AbstractMetricMethodInterceptor.invoke(AbstractMetricMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
at com.ruano.erp.web.rest.FileUploadResource$$EnhancerBySpringCGLIB$$8db88d39.uploadFile(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)

上传代码是下一个:

public String store(MultipartFile file, String folder, String id) {
    try {
        if (file.isEmpty()) {
            throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
        }

        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String newName = id+"."+extension;

        AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials("KEY1", "KEY2"));
        System.out.println("Uploading a new object to S3 from a file\n");
        String fileDestiny = "uploads/"+folder+"/"+newName;

        File convFile = new File(file.getOriginalFilename());
        FileOutputStream fos = new FileOutputStream(convFile); 
        fos.write(file.getBytes());
        fos.close(); 

        s3client.putObject(new PutObjectRequest(s3Bucket, fileDestiny, convFile));

        return newName;
    } catch (Exception e) {
        throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
    }
}

不确定是什么问题,用户也可以完全访问s3和ec2。我不知道该函数是否试图在de elasticbean机器内创建一个临时文件,也许它没有访问权限。

1 个答案:

答案 0 :(得分:1)

最后我做到了,问题是你无法在ElasticBean中的文件系统中写入,所以我替换了这一行:

File convFile = new File(file.getOriginalFilename());
FileOutputStream fos = new FileOutputStream(convFile); 
fos.write(file.getBytes());
fos.close(); 

s3client.putObject(new PutObjectRequest(s3Bucket, fileDestiny, convFile));

有了这个:

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());                

s3client.putObject(new PutObjectRequest(s3Bucket, fileDestiny, file.getInputStream(), metadata));

最后工作!