我正在嘗試通過 Java 從 AWS S3 存盤桶下載 JSON 檔案。
該檔案由名為 Zuora 的第 3 方計費應用程式創建。
第一步是使用 OAuth 憑據生成檔案。然后我得到檔案 URL 的回應。我可以通過瀏覽器訪問它并將其下載到我的桌??面,但是當我嘗試通過 Java 處理檔案時,我遇到了問題。
我在網上到處都看到人們似乎通過使用 AWS 庫中的 AmazonS3Client 克服了類似的問題。參考:
這里不需要信用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
class Swing implements ActionListener {
JFrame frame=new JFrame();
JButton button=new JButton("Click Me");
Swing(){
prepareGUI();
buttonProperties();
}
public void prepareGUI(){
frame.setTitle("My Window");
frame.getContentPane().setLayout(null);
frame.setVisible(true);
frame.setBounds(200,200,400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void buttonProperties(){
button.setBounds(130,200,100,40);
frame.add(button);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
//Get a presigned PDF from an Amazon S3 bucket.
try {
URL url = new URL("<Specify PRESIGNED URL> ") ;
InputStream in = null;
in = url.openStream();
FileOutputStream fos = new FileOutputStream(new File("C:\\AWS\\yourFile2.txt"));
System.out.println("reading from resource and writing to file...");
int length = -1;
byte[] buffer = new byte[1024];// buffer for portion of data from connection
while ((length = in.read(buffer)) > -1) {
fos.write(buffer, 0, length);
}
fos.close();
in.close();
System.out.println("File downloaded");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public class HelloWorldSwing {
public static void main(String[] args)
{
new Swing();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/386885.html
上一篇:使用Object類來表示所有型別的Json負載是一個好主意
下一篇:第七章資料安全
