Thursday, 6 May 2021

deny and allow access to s3 bucket

 https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies.html



{ "Id":"PolicyId2", "Version":"2012-10-17", "Statement":[ { "Sid":"AllowIPmix", "Effect":"Allow", "Principal":"*", "Action":"s3:*", "Resource": [ "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*" ], "Condition": { "IpAddress": { "aws:SourceIp": [ "54.240.143.0/24", "2001:DB8:1234:5678::/64" ] }, "NotIpAddress": { "aws:SourceIp": [ "54.240.143.128/30", "2001:DB8:1234:5678:ABCD::/80" ] } } } ] }


https://s3tools.org/kb/item10.htm



How to restrict access to a bucket to specific IP addresses

To secure our files on Amazon S3, we can restrict access to a S3 bucket to specific IP addresses.

The following bucket policy grants permissions to any user to perform any S3 action on objects in the specified bucket. However, the request must originate from the range of IP addresses specified in the condition. The condition in this statement identifies 192.168.143.* range of allowed IP addresses with one exception, 192.168.143.188.

{
    "Version": "2012-10-17",
    "Id": "S3PolicyIPRestrict",
    "Statement": [
        {
            "Sid": "IPAllow",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::bucket/*",
            "Condition" : {
                "IpAddress" : {
                    "aws:SourceIp": "192.168.143.0/24"
                },
                "NotIpAddress" : {
                    "aws:SourceIp": "192.168.143.188/32"
                }
            }
        }
    ]
}

The IPAddress and NotIpAddress values specified in the condition uses CIDR notation described in RFC 2632. For more information, go to http://www.rfc-editor.org/rfc/rfc4632.txt

Tuesday, 4 May 2021

access S3 bucket without access key and secrete key

 https://stackoverflow.com/questions/44062813/aws-s3-upload-without-access-and-secret-key-in-java

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
              .withCredentials(new InstanceProfileCredentialsProvider(false))
              .build();

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_provider.html
https://stackoverflow.com/questions/52286306/using-ec2-metadata-credentials-in-the-laravel-filesystem

Thursday, 15 April 2021

string-concatenation-vs-stringbuilder-vs-string-format-performance

 https://kylewbanks.com/blog/java-string-concatenation-vs-stringbuilder-vs-string-format-performance



public class TestStrings {

    private static final String PROFILE_PICTURE_URL_BASE = "https://cdn.myapp.com/user/";
    private static final String PROFILE_PICTURE_URL_EXTENSION = ".png";
    private static final String PROFILE_PICTURE_URL = "https://cdn.myapp.com/user/%d.png";

    private static final int NUM_ITERATIONS = 1000000;
    
    public static void main(String[] args) {
        int userId;
        long startTime;
        
        // String Concatentation
        startTime = System.currentTimeMillis();
        for (userId = 0; userId < NUM_ITERATIONS; userId ++) {
            String profilePictureUrl = PROFILE_PICTURE_URL_BASE + userId + PROFILE_PICTURE_URL_EXTENSION;
        }
        System.out.println("String Concatentation: " + (System.currentTimeMillis() - startTime) + "ms");
        
        // StringBuilder
        startTime = System.currentTimeMillis();
        for (userId = 0; userId < NUM_ITERATIONS; userId ++) {
            StringBuilder sb = new StringBuilder(PROFILE_PICTURE_URL_BASE);
            sb.append(userId);
            sb.append(PROFILE_PICTURE_URL_EXTENSION);
            
            String profilePictureUrl = sb.toString();
        }
        System.out.println("StringBuilder: " + (System.currentTimeMillis() - startTime) + "ms");
        
        // String.format
        startTime = System.currentTimeMillis();
        for (userId = 0; userId < NUM_ITERATIONS; userId ++) {
            String profilePictureUrl = String.format(PROFILE_PICTURE_URL, userId);
        }
        System.out.println("String.format: " + (System.currentTimeMillis() - startTime) + "ms");
    }
}

Monday, 12 April 2021

Making dynamic query

 https://stackoverflow.com/questions/8549619/mysql-dynamically-build-query-string-in-a-stored-procedure-based-on-logic



CREATE  PROCEDURE `activate_games_pawan`(IN `gameId` INT(20))

BEGIN   

DECLARE whereCondition varchar(200);

CASE WHEN gameId>0 THEN

   SET @whereCondition =CONCAT(" ","sh_games.game_id=",gameId);

  SELECT @whereCondition;

  ELSE

  SET @whereCondition=1;

    SELECT @whereCondition;

  END CASE;

 SET @query=CONCAT("SELECT * FROM sh_games where ", @whereCondition);

  select @query;

PREPARE stmt FROM @query;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

END

Wednesday, 7 April 2021

Delete duplicate rows from mysql

  SET SQL_SAFE_UPDATES = 0;


    DELETE t1  FROM sh_learning_material AS t1

INNER JOIN sh_learning_material AS t2 

WHERE 

t1.id<t2.id AND

    t1.company_id = t2.company_id;


Thursday, 11 March 2021

Send email with attachment in spring boot

 https://howtodoinjava.com/spring-boot2/send-email-with-attachment/

public void sendMailWithAttachment(String to, String subject, String bodyTextMsg, String fileToAttach) 

{

System.out.println("file loc------------"+fileToAttach);

    MimeMessagePreparator preparator = new MimeMessagePreparator() 

    {

        public void prepare(MimeMessage mimeMessage) throws Exception 

        {

            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

            mimeMessage.setFrom(new InternetAddress(fromEmailId));

            mimeMessage.setSubject(subject);

            

            Multipart multipartContent= new MimeMultipart();

            MimeBodyPart mimeBodyPart =new MimeBodyPart();

            mimeBodyPart.setText(bodyTextMsg);

            MimeBodyPart mimeBodyPartAttachment =new MimeBodyPart();

            mimeBodyPartAttachment.attachFile(fileToAttach);

            multipartContent.addBodyPart(mimeBodyPart);

            multipartContent.addBodyPart(mimeBodyPartAttachment);

            mimeMessage.setContent(multipartContent,"text/csv; charset=utf-8");

            System.out.println("email send sucessfully");

        }

    };

     

    try {

        mailSender.send(preparator);

        System.out.println("email send sucessfully");

        

    }

    catch (MailException ex) {

        // simply log it and go on...

        System.out.println(ex.getMessage());

    }

}


https://coderanch.com/t/271609/java/Java-Mail-send-Multiple-recipients

Monday, 8 March 2021

Read only collection

 https://javarevisited.blogspot.com/2012/07/create-read-only-list-map-set-example-java.html#axzz6obBnjvcF