How to create file in AWS
public void createFile(String bucketName, String existingFilePath) throws IOException
{
 S3Object transectionObject = s3.getObject(new GetObjectRequest(
        bucketName, existingFilePath));
 Bucket bucket=new Bucket(); //Create object of Bucket.
 bucket.setName("bookinginfo"); //Mention bucket name.
 ObjectMetadata metadata = new ObjectMetadata(); // Create object of ObjectMetadata.
   String name=" Yogendrasingh"; // Some text which need to enter in newly created txt file.
    System.out.println(name);
//ByteArrayInputStream for reading text of your file.
   ByteArrayInputStream input = new ByteArrayInputStream(name.getBytes());
    s3.putObject(    bucket.getName(), 
      "AWS file path/"+FileName+".txt", 
      input,metadata
    );
 }
Read and Write File from AWS
public void ReadWriteFile(String bucketName, String existingFilePath) throws IOException
{
 S3Object s3object = s3.getObject(new GetObjectRequest(
        bucketName, existingFilePath));
 BufferedReader reader = new BufferedReader(new InputStreamReader(s3object.getObjectContent()));// This is to read the file.
 
String line;
    String showline="";
    int count=0;
    while((line = reader.readLine()) != null) {
 if(line.isEmpty()==false)
     {      showline=showline+"\r\n"+line;
       count++;
     }
    }
    System.out.println(showline);//File read and saved in variable;
     
   ObjectMetadata metadata = new ObjectMetadata();
   String Text=showline+"\r\n"+”New text”; //New and old text saved in variable
      
   ByteArrayInputStream input1 = new ByteArrayInputStream(Text.getBytes());
      s3.putObject(
     bucketName, 
     "AWS path/"+FileName+".txt", 
     input1,metadata
   );
 reader.close();
}
 
Read property file from AWS
public void checkAccess(String bucketName, String key) throws IOException
{
 S3Object s3object = s3.getObject(new GetObjectRequest(
            bucketName, key));
  InputStream fileName= s3object.getObjectContent();
  //BufferedReader load = new BufferedReader(new InputStreamReader(s3object.getObjectContent()));
  //computerName();
  userAccessFile.load(fileName);
  Set<Object> keys=userAccessFile.keySet();
  newUserflag=keys.contains(UserID);
  AccessStatus=userAccessFile.getProperty(UserID);
  
  //System.out.println(newUserflag);
  //System.out.println(journeyDetails.getProperty("yogendrasingh"));
  //System.out.println(journeyDetails);
  fileName.close();
}
 
No comments:
Post a Comment