将图像上传到AWS S3而不推送到边缘服务器

时间:2020-06-04 07:27:13

标签: java amazon-s3 fileutils

我有一个映射的本地驱动器(X :),它指向一个S3存储桶。

我正在使用以下代码将上传的图片推送到其中。

  public void copyData( MultipartFile file, String dest ) throws RecoverableFileApiException {
    try {
      String path = FilenameUtils.getFullPath( dest );
      File fileDir = new File( path );
      String fileName = FilenameUtils.getName( dest );
      File outFile = new File( fileDir, fileName );

      if ( !fileDir.exists() ) {
        FileUtils.forceMkdir( fileDir );
      }

      file.transferTo(outFile);
      FileUtils.touch( outFile );
      //fileDir.setLastModified( System.currentTimeMillis() );
    }
    catch ( IOException e ) {
      throw new RecoverableFileApiException( log, "There was a problem copying data file to " + dest, e, FILE_UNABLE_TO_COPY_FILE );
    }
  }

它将创建相关目录(X:\ uploads \ category1),并在第一次上传文件时将文件放入其中。文件图像已正确提供(可从浏览器查看)。

当我添加新图像时,它们被放置在同一目录(X:\ uploads \ category1)中,图像也可以正常运行。
但是似乎该映像没有被推送到边缘服务器。
当我通过浏览器查看它时,它将下载图像而不是显示。
在Chrome的“网络”标签中,我可以看到它被用作内容长度为0的“二进制/八位字节流”内容类型。

好像aws文件夹需要在目录级别上踢一下才能让它们拾取添加到目录中的新文件?
当我通过手动将图像复制/粘贴到目录中来进行相同操作时,它将很好地进行服务器处理。

目前暂时无法使用AWS API,除非没有它们当然无法工作。

1 个答案:

答案 0 :(得分:0)

按照约翰的建议,将在以后使用API​​实施。

目前,它的工作方式是:
-使用临时名称在本地驱动器上创建文件
-将文件复制到映射的驱动器
-将映射驱动器上的文件重命名为适当的名称。
-完成,被推送到边缘服务器。

重命名映射驱动器(S3存储桶)上的文件会在下次有人请求时提供新图像。

public void copyDataToCDN( InputStream is, String dest ) throws RecoverableFileApiException {
    FileOutputStream os = null;
    File tmpLocalFile = null;
    try {
      String path = FilenameUtils.getFullPath( dest );
      File fileDir = new File( path );
      String fileName = FilenameUtils.getName( dest );
      File outFile = new File( fileDir, fileName );

      if ( !fileDir.exists() ) {
        FileUtils.forceMkdir( fileDir );
      }

      if ( outFile.exists() ) {
        outFile.delete();
      }

      // Temporarily copy to local file system
      String tmpLocalDir = System.getProperty( "java.io.tmpdir" );
      String tmpDest = tmpLocalDir + "temp_" + fileName;
      String tmpPath = FilenameUtils.getFullPath( tmpDest );
      File tmpFileDir = new File( tmpPath );
      String tmpFileName = FilenameUtils.getName( tmpDest );
      tmpLocalFile = new File( tmpFileDir, tmpFileName );
      os = new FileOutputStream( tmpLocalFile );
      IOUtils.copy( is, os );

      // Copy temp file to Mapped CDN Drive 
      String tmpCDNFileName = "temp_" + fileName;
      File tmpCDNFile = new File( fileDir, tmpCDNFileName );
      FileUtils.copyFile( tmpLocalFile, tmpCDNFile );

      // Rename temp file on Mapped CDN Drives
      File finalCDNFile = new File( fileDir, fileName );
      tmpCDNFile.renameTo( finalCDNFile );
    }
    catch ( IOException e ) {
      throw new RecoverableFileApiException( log, "There was a problem copying data file to " + dest, e, FILE_UNABLE_TO_COPY_FILE );
    }
    finally {
      IOUtils.closeQuietly( is );
      IOUtils.closeQuietly( os );
      if ( tmpLocalFile != null ) {
        tmpLocalFile.delete();
      }
    }
  }