'Uploading file to amazon s3
I am working with a javafx project with google auth2.0 for sign in and amazon s3 for storing files. I wrote the following code for this purpose. I have a file chooser for uploading image to s3. The app works well with sign in but when I try to upload files to s3 it gives some exception which is given below.
main.java
package main;
import controller.signupController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage signinStage,signupStage,mainSatge,decreptStage,profilestage,aws;
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader=new FXMLLoader(getClass().getResource("/view/signinScreen.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
primaryStage.setScene(scene);
scene.getStylesheets().add(Main.class.getResource("/style/styleSheet.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.getIcons().addAll(new Image("/src/Cornie-icons-00.png"));
primaryStage.setResizable(false);
primaryStage.show();
}
public void mainWindow(){
try {
FXMLLoader loader=new FXMLLoader(getClass().getResource("/view/mainScreen.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
mainSatge=new Stage();
mainSatge.setScene(scene);scene.getStylesheets().add(Main.class.getResource("/style/styleSheet.css").toExternalForm());
mainSatge.setScene(scene);
mainSatge.getIcons().addAll(new Image("/src/Cornie-icons-00.png"));
mainSatge.setResizable(false);
mainSatge.show();
}catch (Exception c){
}
}
public void signupWindow(){
try {
FXMLLoader loader=new FXMLLoader(getClass().getResource("/view/signupScreen.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
signupController controller=loader.getController();
controller.main(this, signupStage);
signupStage=new Stage();
signupStage.setScene(scene);scene.getStylesheets().add(Main.class.getResource("/style/styleSheet.css").toExternalForm());
signupStage.setScene(scene);
signupStage.getIcons().addAll(new Image("/src/Cornie-icons-00.png"));
signupStage.setResizable(true);
signupStage.show();
}catch (Exception c){
}
}
public void decreptWindow(){
try {
FXMLLoader loader=new FXMLLoader(getClass().getResource("/view/decreptImageScreen.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
decreptStage=new Stage();
decreptStage.setScene(scene);scene.getStylesheets().add(Main.class.getResource("/style/styleSheet.css").toExternalForm());
decreptStage.setScene(scene);
decreptStage.getIcons().addAll(new Image("/src/Cornie-icons-00.png"));
decreptStage.setResizable(true);
decreptStage.show();
}catch (Exception c){
}
}
public void profilewindow(){
try {
FXMLLoader loader=new FXMLLoader(getClass().getResource("/view/profile.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
profilestage=new Stage();
profilestage.setScene(scene);
profilestage.getIcons().addAll(new Image("/src/Cornie-icons-00.png"));
profilestage.setResizable(false);
profilestage.show();
}catch (Exception c){
}
}
public void aws(){
try {
FXMLLoader loader=new FXMLLoader(getClass().getResource("/view/aws.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
aws=new Stage();
aws.setScene(scene);
aws.getIcons().addAll(new Image("/src/Cornie-icons-00.png"));
aws.setResizable(false);
aws.show();
}catch (Exception c){
}
}
public static void main(String[] args) {
launch(args);
}
}
mainController.java
package controller;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.jfoenix.controls.JFXButton;
import entity.imageEntity;
import java.io.File;
import java.net.MalformedURLException;
import javafx.animation.FadeTransition;
import javafx.animation.TranslateTransition;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;
import main.Main;
import org.kairos.components.RippleViewRow;
import org.kairos.layouts.RecyclerView;
import java.net.URL;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Circle;
import javafx.stage.FileChooser;
public class mainController implements Initializable {
@FXML
private JFXButton historyBTN;
@FXML
private JFXButton addBTN;
@FXML
private JFXButton DecryptimageBTN,logoutBTN;
@FXML
private JFXButton contactsBTN;
@FXML
private JFXButton profileBTN;
@FXML
private JFXButton aboutBTN;
@FXML
private Label nameLB;
@FXML
private Label loggedInEmail;
@FXML
private ImageView imageProfile;
@FXML
private Label usernameLB;
@FXML
private RecyclerView<imageEntity> recycleView;
@FXML
private AnchorPane blackPane;
@FXML
private AnchorPane drawerPane;
@FXML
private Label imageLink;
@FXML
private ImageView drawerIcon;
boolean drawer_show=true;
@FXML
private ImageView imageUpload;
@FXML
private ImageView img;
@Override
public void initialize(URL location, ResourceBundle resources) {
loggedInEmail.setText(Web.getLoggedIn().getDisplayName());
imageLink.setText(Web.getLoggedIn().getBirthday());
usernameLB.setText(Web.getLoggedIn().getGender());
imageProfile.setImage(new Image(Web.getLoggedIn().getImage().getUrl()));
imageProfile.setClip(new Circle(100));
blackPane.setVisible(false);
FadeTransition fadeTransition=new FadeTransition(Duration.seconds(1),blackPane);
fadeTransition.setFromValue(1);
fadeTransition.setToValue(0);
fadeTransition.play();
TranslateTransition translateTransition=new TranslateTransition(Duration.seconds(1),drawerPane);
translateTransition.setToX(-500);
translateTransition.play();
drawerIcon.setOnMouseClicked(event -> {
if (drawer_show==true){
blackPane.setVisible(true);
FadeTransition fadeTransition1=new FadeTransition(Duration.seconds(1),blackPane);
fadeTransition1.setFromValue(0);
fadeTransition1.setToValue(0.12);
fadeTransition1.play();
TranslateTransition translateTransition1=new TranslateTransition(Duration.seconds(1),drawerPane);
translateTransition1.setToX(0);
translateTransition1.play();
drawer_show=false;
drawerIcon.setImage(new Image("/src/left_arrow.png"));
}else {
FadeTransition fadeTransition0=new FadeTransition(Duration.seconds(1),blackPane);
fadeTransition0.setFromValue(0.12);
fadeTransition0.setToValue(0);
fadeTransition0.play();
fadeTransition0.setOnFinished(event1 -> {
blackPane.setVisible(false);
});
TranslateTransition translateTransition0=new TranslateTransition(Duration.seconds(1),drawerPane);
translateTransition0.setToX(-500);
translateTransition0.play();
drawerIcon.setImage(new Image("/src/drawer.png"));
drawer_show=true;
}
});
blackPane.setOnMouseClicked(event -> {
FadeTransition fadeTransition0=new FadeTransition(Duration.seconds(1),blackPane);
fadeTransition0.setFromValue(0.12);
fadeTransition0.setToValue(0);
fadeTransition0.play();
fadeTransition0.setOnFinished(event1 -> {
blackPane.setVisible(false);
});
TranslateTransition translateTransition0=new TranslateTransition(Duration.seconds(1),drawerPane);
translateTransition0.setToX(-500);
translateTransition0.play();
drawerIcon.setImage(new Image("/src/drawer.png"));
drawer_show=true;
});
profileBTN.setGraphic(new ImageView(new Image("/src/ic_account_circle_black_24dp.png")));
historyBTN.setGraphic(new ImageView(new Image("/src/ic_assignment_black_24dp.png")));
contactsBTN.setGraphic(new ImageView(new Image("/src/ic_contacts_black_24dp.png")));
aboutBTN.setGraphic(new ImageView(new Image("/src/ic_error_outline_black_24dp.png")));
DecryptimageBTN.setGraphic(new ImageView(new Image("/src/ic_error_outline_black_24dp.png")));
logoutBTN.setGraphic(new ImageView(new Image("/src/ic_power_settings_new_black_24dp.png")));
DecryptimageBTN.setOnAction(event -> {
new Main().decreptWindow();
});
profileBTN.setOnAction(event -> {
new Main().profilewindow();
});
contactsBTN.setOnAction(event -> {
new Main().aws();
});
Adapter adapter=new Adapter();
imageEntity imageEntity=new imageEntity();
imageEntity.setImageSrc("/src/preview_tom-clancys-rainbow-six-siege-ubisoft-montreal2.jpg");
imageEntity.setName("Image Name: Test Image");
imageEntity.setDate("Date Uploaded: 23-04-2017");
imageEntity.setPrice("Price: $3.5");
recycleView.setAdapter(adapter);
recycleView.getItems().addAll(imageEntity,imageEntity,imageEntity,imageEntity,imageEntity);
addBTN.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
FileChooser fileChooser=new FileChooser();
fileChooser.setInitialDirectory(new File("c:\\"));
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG Images","*.jpg"),
new FileChooser.ExtensionFilter("JPEG Images","*.jpeg"),
new FileChooser.ExtensionFilter("PNG Images","*.png"));
File file=fileChooser.showOpenDialog(null);
if (file!=null){
AWSCredentials Credentials = new BasicAWSCredentials(
"client key",
"client secret");
AmazonS3Client amazonS3Client = new AmazonS3Client(Credentials);
String bucketName = "awsimagetrading";
String key = file.getName();
System.out.println("Uploading a new object to S3 from a file\n");
AmazonS3 s3client = new AmazonS3Client(Credentials);
s3client.putObject(new PutObjectRequest(bucketName,key,file));
URL url = amazonS3Client.generatePresignedUrl(bucketName,key,Date.from(Instant.now().plus(5,ChronoUnit.MINUTES)));
System.out.println(url);
//img.setImage(new Image( file.toURI().toURL().toExternalForm()));
}
}
});
}
public class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
@Override
public RecyclerView.ViewRow call(ListView listView) {
return new RippleViewRow(this);
}
@Override
public Holder onCreateViewHolder(FXMLLoader fxmlLoader) {
fxmlLoader.setLocation(mainController.class.getResource("/view/imageCardViewScreen.fxml"));
Holder holder = new Holder(fxmlLoader);
return holder;
}
@Override
public void onBindViewHolder(Holder holder, Object o) {
imageEntity imageEntity = (imageEntity) o;
holder.imageView.setImage(new Image(imageEntity.getImageSrc()));
holder.name.setText(imageEntity.getName());
holder.price.setText(imageEntity.getPrice());
holder.date.setText(imageEntity.getDate());
}
public class Holder extends RecyclerView.ViewHolder {
@FXML
private Label date;
@FXML
private JFXButton download;
@FXML
private Label price;
@FXML
private Label name;
@FXML
private ImageView imageView;
public Holder(FXMLLoader loader) {
super(loader);
}
}
}
}
Exceptions:
Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: org/apache/http/util/Args
at org.apache.http.conn.scheme.Scheme.<init>(Scheme.java:91)
at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:50)
at com.amazonaws.http.ConnectionManagerFactory.createPoolingClientConnManager(ConnectionManagerFactory.java:29)
at com.amazonaws.http.HttpClientFactory.createHttpClient(HttpClientFactory.java:97)
at com.amazonaws.http.AmazonHttpClient.<init>(AmazonHttpClient.java:164)
at com.amazonaws.AmazonWebServiceClient.<init>(AmazonWebServiceClient.java:119)
at com.amazonaws.AmazonWebServiceClient.<init>(AmazonWebServiceClient.java:103)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:357)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:339)
at controller.AWSUpoladController$1.handle(AWSUpoladController.java:64)
at controller.AWSUpoladController$1.handle(AWSUpoladController.java:47)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.http.util.Args
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
When I use the amazon s3 separately it works fine but it's not working when I try to integrate it with my app. How can I solve this?
Solution 1:[1]
The problem is with library. Just use the appropriate library version and that will solve it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mkrieger1 |
