簡體   English   中英

從亞馬遜s3下載對象時,我無法將其下載到其他文件夾,而不是文件上傳路徑

[英]while downloading the object from amazon s3 i cant able to download it to diffrent folder rather then the file uploaded path

從亞馬遜s3下載對象時,我無法將其下載到不同的文件夾,而不是文件上載路徑...為什么發生這種情況,可能是metaData問題....請發表您的寶貴意見。 。下面是發布上傳和下載代碼的地方

                 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%

由於在Amazon S3中沒有文件夾結構,因此您將所有內容作為對象接收。

例如 :在存儲桶中,您將文件存儲在類似結構的文件夾中,但是當您從S3請求對象時,您將接收文件,例如folder1/folder2/demo.txt

因此嘗試一下,為您的文件獲取S3的InputStream ,如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();
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM