One can tell from the key signature what key a piece is in so long as one knows whether it is major or minor (see “mode”). For example: a key signature of one sharp is either G major or E minor. The best way to find out which it is would be to see whether the end of the piece is based on G major or E minor. Also: if the piece is in the minor key it is very likely that there will be several accidentals in the piece which sharpen the 7th note of the scale (in this case changing a lot of the Ds to D sharps).