Thursday, November 3, 2022

Autoboxing and Unboxing

 

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

Here is the simplest example of autoboxing:

Character ch = 'a';

The rest of the examples in this section use generics. If you are not yet familiar with the syntax of generics, see the Generics (Updated) lesson.

Consider the following code:

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(i);

Although you add the int values as primitive types, rather than Integer objects, to li, the code compiles. Because li is a list of Integer objects, not a list of int values, you may wonder why the Java compiler does not issue a compile-time error. The compiler does not generate an error because it creates an Integer object from i and adds the object to li. Thus, the compiler converts the previous code to the following at runtime:

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(Integer.valueOf(i));

Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.

Consider the following method:

public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i: li)
        if (i % 2 == 0)
            sum += i;
        return sum;
}

Because the remainder (%) and unary plus (+=) operators do not apply to Integer objects, you may wonder why the Java compiler compiles the method without issuing any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:

public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i : li)
        if (i.intValue() % 2 == 0)
            sum += i.intValue();
        return sum;
}

Ref: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html


HashMap vs Hashtable

 

    Hashmap vs Hashtable 

    • HashMap is non-synchronized. It is not thread-safe and can’t be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.
    • HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value.
    • HashMap is generally preferred over HashTable if thread synchronization is not needed.
    Ref: 
    • https://www.geeksforgeeks.org/differences-between-hashmap-and-hashtable-in-java/
    • https://www.javatpoint.com/java-map


    Friday, January 7, 2022

    S3 of AWS

     What is Amazon S3?

    Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. Customers of all sizes and industries can use Amazon S3 to store and protect any amount of data for a range of use cases, such as data lakes, websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics. 

    S3 service is started in 2006.

    Amazon S3 provides management features so that you can optimize, organize, and configure access to your data to meet your specific business, organizational, and compliance requirements.








    Q: How much data can I store in Amazon S3?

    The total volume of data and number of objects you can store are unlimited. Individual Amazon S3 objects can range in size from a minimum of 0 bytes to a maximum of 5 TB. The largest object that can be uploaded in a single PUT is 5 GB. For objects larger than 100 MB, customers should consider using the Multipart Upload capability.




    Amazon S3 Batch Operations







    you can make changes to object metadata and properties, or perform other storage management tasks, such as copying objects between buckets, replacing object tag sets, modifying access controls, and restoring archived objects from S3 Glacier — instead of taking months to develop custom applications to perform these tasks


    S3 Batch Operations is a managed solution for performing storage actions like copying and tagging objects at scale, whether for one-time tasks or for recurring, batch workloads. S3 Batch Operations can perform actions across billions of objects and petabytes of data with a single request. To perform work in S3 Batch Operations, you create a job.







    How to migrate an Amazon S3 bucket from one region to another?

    The short answer is you can’t migrate a S3 bucket from one region to another. But there is a workaround to this.

    Workaround

    1. Create a new bucket in another region. Note that you can not have the same bucket name as your current one because bucket names must be unique.
    2. Copy the contents of the current bucket to the new bucket created in the region you prefer.
    3. Once copied, delete the old bucket.
    4. Replace the bucket name to the old name in the new region if you prefer to maintain the same name for the bucket in the new region. Note that you can only do this after you deleted the old bucket.

    ----------------------------------------------------------------------------------
    What is versioning in S3 AWS?
    Versioning in Amazon S3 is a means of keeping multiple variants of an object in the same bucket. You can use the S3 Versioning feature to preserve, retrieve, and restore every version of every object stored in your buckets. ... Versioning-enabled buckets can help you recover objects from accidental deletion or overwrite.
    ----------------------------------------------------------------------------
    Logs:
    - Server level log
    - Object-level log (need additional cost)
    --------------------------------------------------
    Object lock level functionality. It will protect to delete for unwanted delete operations.
    -------------------------------------------------------
    Block public access (Bucket level)
    Block public access (Account setting)
    ------------------------------
    -------------------------------
    Public accessibility
    -- Able to access Objects by other aws account holder.
    -- public access 
    ------------------------------------------------------------

    So what are presigned URLs anyway?

    A presigned URL is a URL that you can provide to your users to grant temporary access to a specific S3 object. Using the URL, a user can either READ the object or WRITE an Object (or update an existing object). The URL contains specific parameters which are set by your application. A pre-signed URL uses three parameters to limit the access to the user;

    • Bucket: The bucket that the object is in (or will be in)
    • Key: The name of the object
    • Expires: The amount of time that the URL is valid

    -----------------------------------

    S3 bucket propertis




    S3 can be used for static website hosting.


    Scale up automatically

    Without making any changes to your initial setup AWS S3 will automatically scale up the infrastructure to meet the growing demand.

    3. High Availability

    Amazon guarantees 99.99% availability of S3 which means there is almost no chance of losing your data. S3 achieves this by replication across multiple data centers

    4. Fast Content Serving with Amazon CloudFront

    If you have globally distributed audience then CloudFront can help you deliver the contents in a very efficient manner. CloudFront has 70 data centres, called edge locations, all across the globe. Content of your website are cached at these edge locations and every visitor is served via the nearest edge location hence decreasing the latency and resulting in optimum response time.

    5. Negligible costs

    Hosting your small to medium sized static website would cost you few dollars monthly. For example see the sample costing provided below

    S3 Standard Storage: 1 GB

    PUT and other similar requests: 30000

    GET and other similar requests: 30000

    Data transfer out: 3 GB

    Data transfer in: 3 GB

    Route53 Hosted Zones: 1

    Standard Queries to Route53: 1 million per month

    Cost per month: $1.30


    S3 Transfer Acceleration

    Amazon S3 Transfer Acceleration is a bucket-level feature that enables fast, easy, and secure transfers of files over long distances between your client and an S3 bucket. Transfer Acceleration takes advantage of the globally distributed edge locations in Amazon CloudFront.

    Terabyte and terabyte data download or upload, we can use this service. You have to aware of cost. You have to carry very very high cost. it not cheap.


    What is an S3 event?

    The Amazon S3 notification feature enables you to receive notifications when certain events happen in your bucket. Whenever an action is taken on an s3 object, an event is created. This event can send notifications to SQS, SNS or AWS Lambda service. These events can be used to enable event-driven workflows.


    Requester Pays

    Amazon S3 buckets configured for Requester Pays means that the requester will be charged for Data Transfer costs. Since these costs need to be charged back to an identified AWS Account, the objects must be accessed via authenticated requests.



    How To Secure S3 Buckets Effectively


    Tip 1: Securing Your Data Using S3 Encryption



    • Server-Side Encryption: Using this type of encryption, AWS encrypts the raw data you send and stores it on its disks (on data centers). When you try to retrieve your data, AWS reads the data from its disks, decrypts, and sends it back to you.
    • Client-Side Encryption: Using this type of encryption, instead of AWS, it’s you who encrypts the data before sending it to AWS. Once you retrieve the data from AWS, you need to decrypt it.



    Tip 2: Managing Access Control

    -

    Tip 3: Maximizing S3 Reliability With Replication


    1. S3 versioning:

    Tip 4: Enforcing SSL

    Tip 5: Enhancing S3 Security Using Logging


    Tip 6: Putting S3 Object Locking To Work


    Tips 7:  presign url

    tips8 






    REf:
    • https://www.youtube.com/watch?v=7M3s_ix9ljE
    • https://www.youtube.com/watch?v=L3dYocCSU-E
    • https://medium.com/panther-labs/how-to-secure-s3-buckets-effectively-9c1a3a7178bb
    • https://www.youtube.com/watch?v=IUdkEuvihOk
    • https://aws.amazon.com/blogs/security/iam-policies-and-bucket-policies-and-acls-oh-my-controlling-access-to-s3-resources/










    Ref:
    1. https://www.youtube.com/watch?v=L3dYocCSU-E

    Sunday, January 2, 2022

    VPC VPN - site-to-site VPN and client VPN

     VPN

    AWS Virtual Private Network solutions establish secure connections between your on-premises networks, remote offices, client devices, and the AWS global network. 

    AWS VPN is comprised of two services: AWS Site-to-Site VPN and AWS Client VPN. 

    Each service provides a highly-available, managed, and elastic cloud VPN solution to protect your network traffic.


    Site-to-Site VPN:

    - customer getway and virtual private gate wate (VGW)

    - you need route table to define the vgw_id in datacenter and customer getway id in vpc 


    Client VPN

    - need client vpn software in your laption like open vpn software



    Ref:

    1. https://aws.amazon.com/vpn/
    2. https://www.youtube.com/watch?v=St8y0xZSn3c (clinet vpn)
    3. https://www.youtube.com/watch?v=7tTrN8WXMlg  (site to site vpn)
    4. https://www.scalefactory.com/blog/2020/12/14/how-to-set-up-a-site-to-site-vpn-connection/ (site to site configuration)
    5. https://docs.aws.amazon.com/vpc/latest/userguide/vpn-connections.html 




    VPC limits

     

    - 5 vpc per region

    - 200 subnet per vpc

    - 200 route table


    Security group and NACL


    What are AWS Security Groups? 

    An AWS security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Both inbound and outbound rules control the flow of traffic to and traffic from your instance, respectively.


    - security group on instance level

    - security group on database level


     














    REf:
    https://www.youtube.com/watch?v=fpxDGU2KdkA  (from 33 min)





    What is a Security Group?

    It adds a security layer to EC2 instances that control both inbound and outbound traffic at the instance level.

    What is NACL?

    NACL also adds an additional layer of security associated with subnets that control both inbound and outbound traffic at the subnet level.

    Combining Security Group and NACL

    Maximum number of rules that exist per NACL: 20

    Maximum number of rules that can exist per Security Group: 50

    How to find Nth Highest Salary in SQL

    Maximum number of Security Groups that can exist per instance: 5

    Maximum number of rules that can exist per instance: 5*50 + 20 = 270

    Differences b/w Security Group and NACL

    What is a Security Group
    Security GroupNACL (Network Access Control List)
    It supports only allow rules, and by default, all the rules are denied. You cannot deny the rule for establishing a connection.It supports both allow and deny rules, and by default, all the rules are denied. You need to add the rule which you can either allow or deny it.
    It is a stateful means that any changes made in the inbound rule will be automatically reflected in the outbound rule. For example, If you are allowing an incoming port 80, then you also have to add the outbound rule explicitly.It is a stateless means that any changes made in the inbound rule will not reflect the outbound rule, i.e., you need to add the outbound rule separately. For example, if you add an inbound rule port number 80, then you also have to explicitly add the outbound rule.
    It is associated with an EC2 instance.It is associated with a subnet.
    All the rules are evaluated before deciding whether to allow the traffic.Rules are evaluated in order, starting from the lowest number.
    Security Group is applied to an instance only when you specify a security group while launching an instance.NACL has applied automatically to all the instances which are associated with an instance.
    It is the first layer of defense.It is the second layer of defense.

    Autoboxing and Unboxing

      Autoboxing  is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper cl...