I'm trying to upload my object to s3 bucket, but no response is getting back from client builder.
CompletableFuture.runAsync(()->{
try {
//runs up to here
System.out.println("start connection");
AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(AppConstants.AWS_REGION).build();
System.out.println("connected");
//nothing executed below!
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(mimeType);
metadata.addUserMetadata("x-amz-meta-title", "someTitle");
metadata.setContentLength(bytes.length);
PutObjectRequest request = new PutObjectRequest(bucket, fileName, bais , metadata);
request.setCannedAcl(CannedAccessControlList.BucketOwnerFullControl);
s3.putObject(request);
}catch (AmazonServiceException e) {
//no errors logged
e.printStackTrace();
}catch (SdkClientException e) {
// no errors logged
e.printStackTrace();
}
catch(Exception e) {
//no errors logged
e.printStackTrace();
}
});
The server is logging the following:
(ForkJoinPool.commonPool-worker-5) start connection
and then nothing happens
PS: Libraries I'm using:
aws-java-sdk-core-1.11.482
aws-java-sdk-s3-1.11.482
I also have my credential file in ~/.aws/credentials
The aws-java-sdk internally uses joda-time library which was missing and not connecting, I have included it and it worked.
Related
Iam currently struggling a little bit with the FTPSClient from Apache Commons. See code down below. I try to write a file to FTP Server (vsftpd) when using FTPClient things are working perfectly fine. When using my code snippet I will always get a 451 Error, when debugging and waiting after Util.copyStream() returned everything works fine or settings a Thread.sleep(100). This also does not happen when I do not set the ftpsClient.execProt("P). Does anyone know by what this could be caused.
final FTPSClient client;
client = new FTPSClient("TLS", false);
client.setUseClientMode(true);
client.setDefaultPort(21);
// connect
try {
client.connect("serverAddress", 21);
} catch (SSLException e) {
throw e;
}
// setup any after connected
client.setSoTimeout(300);
client.setListHiddenFiles(true);
client.enterLocalPassiveMode();
FTPClientConfig ftpConfig;
try {
ftpConfig = new FTPClientConfig(client.getSystemType());
} catch (FTPConnectionClosedException e) {
throw e;
} catch (IOException e) {
ftpConfig = new FTPClientConfig();
}
client.configure(ftpConfig);
final FTPSClient ftpsClient = client;
// remove data buffer limit
ftpsClient.execPBSZ(0);
// set data channel encrypted
ftpsClient.execPROT("P");
client.login("user", "password");
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("Authentication failed: " + client.getReplyString().trim());
}
// postconfigure connection
if (!client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE) || !client.setFileType(FTP.BINARY_FILE_TYPE)) {
throw new IOException("Failed to correctly configure client: " + client.getReplyString().trim());
}
InputStream input;
OutputStream output;
input = new FileInputStream(pathToLocalFile);
output = client.storeFileStream("foobar.txt");
final var number = Util.copyStream(input, output);
System.out.println(number);
input.close();
// Thread.sleep(100);
output.close();
// Must call completePendingCommand() to finish command.
if (!client.completePendingCommand()) {
client.logout();
client.disconnect();
System.err.println("File transfer failed.");
}
This library has been around for a long time, and things change a bit under the hood. Try:
input = new FileInputStream(pathToLocalFile);
boolean result = client.storeFile("foobar.txt", input);
if (result) {
System.out.println("\tFile Transfer Completed Successfully");
}
I have noticed that every once in a while when transferring files to a mainframe, it won't complete. I think it has something to do with the file length, but I've never been able to track it down. I also don't use the stream_transfer_mode.
I'm having some trouble with transferring files to my ftp.
with this code i get no errors, but I also don't get a file to my ftp server. I hope some more experienced users can point me to the right direction.
FTPClient client = new FTPClient();
FileInputStream picture = null;
try {
client.connect("adress" ,port);
client.login("user", "password");
client.changeWorkingDirectory("/htdocs/javaprojekt");
client.setFileType(FTP.BINARY_FILE_TYPE);
String filename = "/Users/sicknk/Documents/Java Projekt/fyeah.jpg";
picture = new FileInputStream(filename);
client.storeFile(filename, picture);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (picture != null) {
picture.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have just such a problem, the following small code image downloader where params[0]- url and params[2] - file name, this design works well on Android 5.0+, but on older versions, I get an error:
-javax.net.ssl.SSLException: Connection closed by peer
#Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
InputStream is = null;
try {
is = new URL(params[0]).openStream();
} catch (IOException e) {
e.printStackTrace(); // Exception there
}
bitmap = BitmapFactory.decodeStream(is);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
OutputStream outStream = null;
File file = new File(Constans.DATA_APPLICATION_PATH+"/photo/"+params[1].replace("\"","").trim());
try {
// make a new bitmap from file
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
file.deleteOnExit();
return bitmap;
}
`
You have problem with SSL authorization, which will come for sure if you are using self signed certificates.
So the way to make android app to connect to URL is to skip the SSL verification (Only if we are sure about the connection)
For this you can add this class and execute it inside onCreate method
new NukeSSLCerts().nuke();
It will skip certificate verification by trusting all certificates.
So I'm uploading a file to my VPS (Linux Centos 5 64 bit) via FTP using Java. The code I'm using to upload to my VPS is
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
Now the code is working but what I want is to change where on the VPS it uploads the file to. Right now it's
serverip/HERE
I have some files so want to change it to
serverip/file/HERE
How can I go about doing so?
You can use the changeCurrentWorkingDirectory() method to change to the desired directory. Once you're in there, you can write the file using storeFile just like before.
changeCurrentWorkingDirectory returns true if the directory change was successful, otherwise it returns false. It takes a string which interpreted as the directory path. If the path starts with a slash, it's interpreted as absolute path starting at the ftproot directory. Otherwise it's interpreted as relative path.
Revised code could look something like this:
FTPClient client = new FTPClient();
FileInputStream fis = null;
try
{
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// change directory to serverip/file/
if (client.changeWorkingDirectory("/file"))
{
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
}
client.logout();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
hey i want to upload a file to a directory called 'screenshots' on my webserver via FTP using java. I have been using this code and it says that it stores the file successfully and connected successfully but when i check my screenshots directory via the cpanel i dont see the file that was uploaded any help?
public static void uploadFilee() {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("****************");
client.login("********", "********");
System.out.println("Connected Successfully");
String filename = "C:/Users/Christian/Desktop/screenshots/img_" + queueInfo.get("SessionID");
fis = new FileInputStream(filename);
client.storeFile(filename, fis);
System.out.println("Stored File Successfully");
client.logout();
} catch (IOException e) {
System.out.println("Error_1");
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
System.out.println("Error_2");
e.printStackTrace();
}
}
}
`
You may want to review this page
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#_storeFile(java.lang.String,java.lang.String,java.io.InputStream)
you have your statements such as storefile in a try statement, but if they fail, they return false, not an exception.
changing your code to inspect the return values of each function should help you find where your problem lies.