从亚马逊s3下载对象时,我无法将其下载到不同的文件夹而不是文件上传路径

时间:2014-11-20 10:14:08

标签: java amazon-s3

从亚马逊s3下载对象时,我无法将其下载到不同的文件夹,而不是文件上传的路径...为什么它发生这样可能是元数据问题....请发表您的宝贵意见谢谢提前..我们正在发布上传和下载的代码

                 public void AmazonUpload(String fileObj) throws IOException {
    try {
        this.key = fileObj;
        try {
            if (this.key == null) {

            } else {
                if (readFile(this.key) != null) {
                   // this.key="1";
                    this.putObjResult = this.amzObj.putObject(new PutObjectRequest(this.bucketName, this.key, readFile(this.key)));
                    }
            }
        } catch (AmazonServiceException ae) {
            System.out.println(ae.getMessage());
        }
    } catch (AmazonServiceException ex) {
        Logger.getLogger(AthinioCloudMigration.class.getName()).log(Level.SEVERE, null, ex);
    }
}
           public void AmazonDownload(String dirName, String xmlFilename, String amazonid) throws ParserConfigurationException, SAXException, TransformerException, IOException {
    String cloudid;
    cloudid = amazonid;
    this.comm = new CommonResources(xmlFilename);

    this.RequestFiles=new ArrayList();
    try {
        this.RequestFiles = this.comm.getXML(cloudid);
        if (this.RequestFiles != null) {
             int len = this.RequestFiles.size();
             System.out.println(len);
            for (int index = 0; index < len; index++) {
                this.CRobj = (CommonResources) this.RequestFiles.get(index);
                if (cloudid.equals(this.CRobj.getCloudID())) {
                  this.newFile = new File(dirName + this.CRobj.getFileName().concat(".rec"));
                   System.out.println(newFile);
                   newFile.createNewFile();
                    this.metaData = this.amzObj.getObject(new GetObjectRequest(this.bucketName, (dirName + this.CRobj.getFileName())), this.newFile);
                    System.out.println(metaData);
                     java.io.File tmp = new java.io.File(dirName + this.CRobj.getFileName());
                    System.out.println(tmp);
                       tmp.delete();
                                                                           76,23         87%

1 个答案:

答案 0 :(得分:0)

由于在Amazon S3中没有文件夹结构,因此您将所有内容都作为对象接收。

例如:在您的存储桶中,您将文件存储在类似于结构的文件夹中,但是当您从S3请求对象时,您将收到folder1/folder2/demo.txt之类的文件。

请尝试使用此功能,为InputStream等文件获取S3的amazonS3.getObject(bucket, "folder1/folder2/demo.txt").getObjectContent();。获取InputStream后,将您的文件名,下载位置和InputStream传递给以下方法。如果您使用Java 7使用FileSystems.getDefault().getPath(fullPathWithFileName).getFileName().toString()从对象名称中获取文件名。

        public void saveFile(String uploadFileName, String path, InputStream inputStream) throws Exception {
        DataOutputStream dos = null;
        OutputStream out = null;
        try {
            File newDirectory = new File(path);
            if (!newDirectory.exists()) {
                newDirectory.mkdirs();
            }

            File uploadedFile = new File(path, uploadFileName);
            out = new FileOutputStream(uploadedFile);
            byte[] fileAsBytes = new byte[inputStream.available()];
            inputStream.read(fileAsBytes);

            dos = new DataOutputStream(out);
            dos.write(fileAsBytes);
        } catch (IOException io) {
            io.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }